Friday, 5 April 2013

String examples & JdbcBundle examples in Java


class StringTest
{
public static void main(String[] args)
{
String s1 = new String("You cannot change me");
String s2 = new String("You cannot change me");
System.out.println(s1==s2);//false
String s3 = "You cannot change me";
System.out.println(s1==s3);//false
String s4 = "You cannot change me";
System.out.println(s3==s4);//true
String s5 = "You cannot"+" change me";
System.out.println(s4==s5);//true
String s6 = "You cannot";
String s7 = s6 + " change me";
System.out.println(s4==s7);//false
final String s8 = "You cannot";
String s9 = s8 + " change me";
System.out.println(s4==s9);//true
}
}
=======================================
//CountWords
import java.util.Scanner;

public class CountWords
{
    public static void main (String[] args)
    {
         Scanner sc=new Scanner(System.in);
         System.out.println("Please enter string: ");
         String str=sc.nextLine();
            int wordCount = 1;
            for (int i = 0; i < str.length(); i++)
            {
                if (str.charAt(i) == ' ')
                {
                    wordCount++;
                }
            }
            System.out.println("Word count is = " + wordCount);
    }
}
 ============================
 //CountWordsUsingString.java
import java.util.Scanner;
import java.util.StringTokenizer;

public class CountWordsUsingString {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter string: ");
String st = sc.nextLine();
int count = 0;
StringTokenizer stk = new StringTokenizer(st, " ");
while (stk.hasMoreTokens()) {
String token = stk.nextToken();
count++;
}
System.out.println("Number of words are: " + count);
}
}
 ============================
class StringTest
{
public static void main(String[] args)
{
String s1 = new String("You cannot change me");
String s2 = new String("You cannot change me");
System.out.println(s1==s2);//false
String s3 = "You cannot change me";
System.out.println(s1==s3);//false
String s4 = "You cannot change me";
System.out.println(s3==s4);//true
String s5 = "You cannot"+" change me";
System.out.println(s4==s5);//true
String s6 = "You cannot";
String s7 = s6 + " change me";
System.out.println(s4==s7);//false
final String s8 = "You cannot";
String s9 = s8 + " change me";
System.out.println(s4==s9);//true
}
} ============================
 //CountCharacterUsingHashMap.java

import java.util.HashMap;
import java.util.Scanner;

public class CountCharacterUsingHashMap {

public static void main(String[] args) {
// String str = "Character duplicate word example java";
Scanner sc = new Scanner(System.in);
System.out.println("Please enter string: ");
String str = sc.nextLine();
char ch1[] = str.toCharArray();
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();

for (int i = 0; i < ch1.length; i++)
{
char ch = ch1[i];
hm.put(ch, (hm.get(ch) == null ? 1 : hm.get(ch) + 1));
}
System.out.println("No of Characters are : " + hm.toString());
}
}
 =============================
  //CountSelectedWord.java
import java.util.HashMap;
import java.util.StringTokenizer;

public class CountSelectedWord {
       public static void main(String[] args) {
  
        String str = "java java android android java example";
        String selectedword="java";
       
        StringTokenizer t = new StringTokenizer(str);
        HashMap<String,Integer> hms=new HashMap<String,Integer>();
      
              while(t.hasMoreTokens())
              {
                     String word = t.nextToken();
                      hms.put(word,(hms.get(word)==null?1:hms.get(word)+1));
              }
              System.out.println(selectedword+" contents  " + hms.get(selectedword)+ " times.");
     
       }
}
 ===========================
 //CountSelectedCharacter.java
import java.util.HashMap;
public class CountSelectedCharacter {
       public static void main(String[] args) {
        String str = "Count Selected Character example in java";
        char selectedchar='C';
        char ch1[]=str.toUpperCase().toCharArray();
        HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
        for(int i=0; i<ch1.length; i++)
        {
            char ch=ch1[i];
            hm.put(ch,(hm.get(ch)==null?1:hm.get(ch)+1));
        }
      
        System.out.println(selectedchar+" contents  " + hm.get(selectedchar)+ " times.");
      
       }
}
 ============================
import java.util.ArrayList;

class stringreplace
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
String s = new String("swamy narayana raju");
String t = s.replace("", "\n");
System.out.println(t);
}
}
 ==================================
import java.io.File;

class filecreat
{
public static void main(String[] args) throws Exception
{
File f = new File("cba.txt");
System.out.println(f.exists()); // false at first time.
f.createNewFile();
System.out.println(f.exists()); // true
}
}
 ====================================
class StaticDemo
{
static int i = m1();
public static int m1()
{
System.out.println("Hello ....I am able to print");
System.exit(0);
return 1;
}
}
 ====================================
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Dog implements Serializable
{
transient Cat c = new Cat();
}
class Cat
{
int j = 20;
}
class SerializeDemo
{
public static void main(String arg[]) throws Exception
{
Dog d = new Dog();
System.out.println("Before Serialization:" + d);
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d1 = (Dog) ois.readObject();
System.out.println(d1);
}
}
 ======================
//prop1.java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;

class prop1
{
public static void main(String[] args)
{
try
{
Properties p = new Properties();
String pfile;
FileInputStream fis = new FileInputStream("pfile");
p.load(fis);
String sno = p.getProperty("stno");
String name = p.getProperty("sname");
String marks = p.getProperty("smarks");
System.out.println("Student number" + sno);
System.out.println("Student name" + name);
System.out.println("Student marks" + marks);
fis.close();
}
catch (ArrayIndexOutOfBoundsException ae)
{
System.err.println("plese enter properties file");
}
catch (FileNotFoundException fe)
{
System.err.println("the properties file id not found,pleace enter correct");
}
catch (Exception e)
{
System.err.println(e);
}
}
}
 =====================================
import java.io.File;
import java.io.FileWriter;

class filewrit
{
public static void main(String arg[]) throws Exception
{
File f = new File("pongal.txt");
System.out.println(f.exists());// false
FileWriter fw = new FileWriter(f, true);
System.out.println(f.exists());
fw.write(97);
fw.write("run\nsoftware\n");
char[] ch1 = { 'a', 'b', 'c' };
fw.write(ch1);
fw.flush();
fw.close();
}
}
 =======================================
import java.io.File;
import java.io.FileReader;

class fileread
{
public static void main(String arg[]) throws Exception
{
File f = new File("pongal.txt");
FileReader fr = new FileReader(f);
System.out.println(fr.read());
char[] ch2 = new char[(int) (f.length())];
System.out.println(ch2.length);
fr.read(ch2);
for (char ch1 : ch2)
{
System.out.print(ch1);
}
}
}
 =========================================
final class Immutable
{
private int i;
      Immutable(int i)
      {
            this.i=i;
      }
      public Immutable instance(int i)
      {
            if(this.i==i)
            {
                  return this;
            }
            else
            {
                  Immutable k=new Immutable(i);
                  return k;
            }
      }
      public static void main(String[] args)
      {
            Immutable im1=new Immutable(10);
            Immutable im2=im1.instance(10);
            Immutable im3=im1.instance(100);
        System.out.println(im1);
         System.out.println(im2);
             System.out.println(im3);
             /*System.out.println(im1.hashCode());
         System.out.println(im2.hashCode());
             System.out.println(im3.hashCode());*/
         System.out.println(im1==im2);
             System.out.println(im1==im3);
      }
}
 ==================================
class ReverseString
{
      public static void main(String[] args)
      {
            String s="swamy";
            String s1=" ";
            for(int i=s.length()-1;i>=0;i--)
            {
                  s1=s1+s.charAt(i);
                 
            }
            System.out.println(s1);
      }
}
 ======================================
 //StringPalindrome.java
import java.util.*;
public class StringPalindrome {
       public static void main(String args[])
          {
          
             Scanner sc=new Scanner(System.in);
             System.out.println("Please enter string: ");
             String st=sc.nextLine();
                   String rev="";      
             int length = st.length();
             for (int i = length - 1 ; i >= 0 ; i--)
                rev = rev + st.charAt(i);
             if (st.equals(rev))
                System.out.println(st+" is a palindrome.");
             else
                System.out.println(st+" is not a palindrome.");
          }
}
 ============================================

package com.shris.Springboot.tutorial.config;

class EachWordReverse {

public static void main(String[] args) {

String input = "swamy good boy";

StringBuilder result = new StringBuilder();


for (String word : input.split(" ")) {

result.append(new StringBuilder(word).reverse()).append(" ");

}


System.out.println(result.toString().trim());

}

}

============================================

class AllWordsReverse {

public static void main(String[] args) {

String input = "swamy good boy";

String[] words = input.split(" ");

StringBuilder result = new StringBuilder();


for (int i = words.length - 1; i >= 0; i--) {

result.append(words[i]).append(" ");

}


System.out.println(result.toString().trim());

}

}

  ======================================

import java.util.StringTokenizer;


class StringToken {

public static void main(String[] args) {

String s = "swamy*good*boy";

StringTokenizer st = new StringTokenizer(s, "*");


while (st.hasMoreTokens()) {

System.out.println(st.nextToken());

}

}

}

 ======================================

public class Token {

public static void main(String[] args) {

String s = "www.microsoft007.in";

String[] tokens = s.split("\\.");

for (String token : tokens) {

System.out.println(token);

}

}

}

 ==================================================

import java.util.*;

import java.io.*;

class JdbcBundleTest

{

public static void main(String[] args) throws Exception

{

Properties p = new Properties();

FileInputStream fis = new FileInputStream("jdbcdata.properties");

p.load(fis);

String s1 = p.getProperty("jdbc.driver");

String s2 = p.getProperty("jdbc.url");

String s3 = p.getProperty("jdbc.user");

String s4 = p.getProperty("jdbc.pwd");

Class.forName(s1);

Connection con = DriverManager.getConnection(s2, s3, s4);

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("select *from sathya124");

while (rs.next())

{

System.out.println(rs.getInt(1) + "" + rs.getString(2));

}

rs.close();

stmt.close();

con.close();

fis.close();

}

}
                          ##################################
                  #jdbcdata.properties
                                        jdbc.driver=oracle.jdbc.OracleDriver
                                        jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
                                        jdbc.user=scott
                                         jdbc.pwd=tiger