Tuesday, September 18, 2012

StringBuilder Constructor with default argument as a String variable (i.e), initialized to the contents of the specified string, public StringBuilder(String str)

StringBuilder(String str)
Constructs a string builder initialized to the contents of the specified string.
The initial capacity of the string builder is 16 plus the length of the string argument.

Parameters:
str - the initial contents of the buffer.
Throws:
NullPointerException - if str is null

public class StringBuilderConstructorofSpecifiedString {
    public static void main(String[] args) {

        StringBuilder sb1 = new StringBuilder("1");
        System.out.println("Initial Capacity of sb1 : " + sb1.capacity());
        System.out.println("Initial Size of sb1 : " + sb1.length());       
       
        sb1.append("2");
        System.out.println("Capacity of sb1 After 2 Addition  : " + sb1.capacity());
        System.out.println("Size of sb1 After 2 Addition : " + sb1.length());       
       
        sb1.append("123456789012345");
        System.out.println("Capacity of sb1 When it Equals Seventeen Numbers : " + sb1.capacity());
        System.out.println("Size of sb1 When it Equals Seventeen Numbers : " + sb1.length());       

        sb1.append("6");
        System.out.println("Capacity of sb1 When it exceeds Seventeen Numbers : " + sb1.capacity());
        System.out.println("Size of sb1 When it exceeds Seventeen Numbers : " + sb1.length());           
       
        StringBuilder sb2 = new StringBuilder("");
        System.out.println("Initial Capacity of sb2 : " + sb2.capacity());
        System.out.println("Initial Size of sb2 : " + sb2.length());

        sb2.append("1");
        System.out.println("Capacity of sb2 After One Additions : " + sb2.capacity());
        System.out.println("Size of sb2 After One Additions : " + sb2.length());
       
        sb2.append("1234567890123456");
        System.out.println("Capacity of sb2 When it exceeds Sixteen Numbers : " + sb2.capacity());
        System.out.println("Size of sb2 When it exceeds Sixteen Numbers : " + sb2.length());
       
        sb2.append("1234567890123456789");
        System.out.println("Capacity of sb2 When it exceeds ThirtyFour Numbers : " + sb2.capacity());
        System.out.println("Size of sb2 When it is less than or Equal to ThirtyFour Numbers:"+sb2.length());          
    }
}

The  Answers Are :
----------------------
(a1)
Initial Capacity of sb1 : 17
        Initial Size of sb1 : 1

(b1)


Capacity of sb1 After 2 Addition  : 17
        Size of sb1 After 2 Addition  :
2
(c1)
Capacity of sb1 When it Equals Seventeen Numbers : 17
        Size of sb1 When it Equals Seventeen Numbers : 17

(d1)

Capacity of sb1 When it exceeds Seventeen Numbers : 36
        Size of sb1 When it exceeds Seventeen Numbers : 18

 

(a2)
Initial Capacity of sb2 : 16
        Initial Size of sb2 : 0

(b2)

Capacity of sb2 After One Additions : 16
        Size of sb2 After One Additions : 1

(c2)

Capacity of sb2 When it exceeds Sixteen Numbers : 34
        Size of sb2 When it exceeds Sixteen Numbers : 17

(d2)

Capacity of sb2 When it exceeds ThirtyFour Numbers : 70
Size of sb2 When it is less than or Equal to ThirtyFour Numbers : 36

 

Monday, September 17, 2012

StringBuilder() Constructor with no characters in it

public class StringBuilderConstructor {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
       
        //(a)
        System.out.println("Initial Capacity : " + sb.capacity());
        System.out.println("Initial Size : " + sb.length());
       
        sb.append("1234567890123456");
      
        //(b)
        System.out.println("Capacity After Sixteen Additions : " + sb.capacity());
        System.out.println("Size After Sixteen Additions : " + sb.length());
       
        sb.append("7");
      
        //(c)
        System.out.println("Capacity When it exceeds Sixteen Numbers : " + sb.capacity());
        System.out.println("Size When it exceeds Sixteen Numbers : " + sb.length());
       
        sb.append("12345678901234567");
      
        //(d)
        System.out.println("Capacity When it Equals ThirtyFour Numbers : " + sb.capacity());
        System.out.println("Size When it is less than or Equal to ThirtyFour Numbers : " + sb.length());       
   
    }
}

The  Answers Are :
----------------------
(a)
Initial Capacity : 16
       Initial Size : 0

 
(b)
Capacity After Sixteen Additions : 16
       Size After Sixteen Additions : 16
 
(c)
Capacity When it exceeds Sixteen Numbers : 34
       Size When it exceeds Sixteen Numbers : 17
 
(d)
Capacity When it exceeds ThirtyFour Numbers : 34
Size When it is less than or Equal to ThirtyFour Numbers : 34

Sunday, September 16, 2012

StringBuilder Constructors

StringBuilder has 4 Constructor types for it

They are
1) StringBuilder()
2) StringBuilder(CharSequence seq)
3) StringBuilder(int capacity)
4) StringBuilder(String str)

After Writing the Program, in the Middle of the Constructors press CTRL + SPACE button toghether
Now We Can able to See which are and all the types of constructors available in StringBuilder.








Saturday, September 15, 2012

How to Convert Ascii to Character in Java ?

        (a)  
        For Example for Converting ascii to character in Java use the following code
        char characterValue = (char)asciiValue;
       
        class CharacterValue
        {
               public static void main(String[] args) {
               int asciiValue='100';
               char characterValue = (char)asciiValue;
               /*
                  The Above Statement Shows that the ascii value will be converted into its
                  Corresponding Character Value and hence the char character = (char)ascii;
                  will print out the value as 'd'
              */
               System.out.println("CharacterValue for ascii 100 is : " + characterValue);
               }
        }
       
        Answer : 
        CharacterValue for ascii 100 is : 'd'

        (b)
        For Example for Converting character to ascii in Java use the following code
        int asciiValue = (int)characterValue;
       
        class AsciiValue
        {
               public static void main(String[] args) {
               char characterValue='d';
               int asciiValue = (int)characterValue;
               /*
                  The Above Statement Shows that the character value will be converted into its
                  Corresponding Ascii Value and hence the int asciiValue = (int)characterValue;
                  will print out the value as '100'
              */
               System.out.println("AsciiValue for character 'd' is : " + asciiValue);
               }
        }
       
        Answer : 
        AsciiValue for character 'd' is :100

Saturday, September 8, 2012

Sample code for Sorting String in ascending order

Try out these Programs

(a)
package pack1;
import java.util.*;
import java.lang.*;
import pack1.Orderly;
public class Example{
   public static void main(String args[]){
        String name = "";
        ArrayList list=new ArrayList();
        Orderly orderly = new Orderly ();
        try
        {
              list=orderly.displayRecordNew();
              Collections.sort(list);
              /*
                 Sorts the array list
                 If We have to Sort the Name in Descending Order then we have to uncomment the 
                 below commented lines.
                 Comparator r = Collections.reverseOrder();
                 Collections.sort(list,r);
              */
              Iterator l1 = list.iterator();        
              while(l1.hasNext()){
                  orderly  = (Orderly)l1.next();
                  name = orderly.getOrderlyName();
                  System.out.println("Name is " + name);
              }
        }
        catch(Exception e)
        {
        }
   }
}

(b)
package pack1;
import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
class Orderly implements Comparable<Orderly>{
    public String nameDesig = "a";
    private String name;
    public void setOrderlyName(String name){
        this.name = name;
    }
    public String getOrderlyName(){
        return name;
    }
    //Overriding the compareTo method
    public int compareTo(Orderly o){
        return (this.name).compareTo(o.name);
    }       
    public ArrayList displayRecordNew() throws Exception
    {
        File file = new File("C://Users//Radio1//Desktop//Names.txt");
        FileInputStream fis = null;
        StringTokenizer st;

        ArrayList showRecord=null;           
        try {
            showRecord=new ArrayList();               
            fis = new FileInputStream(file);
            /*
               FileInputReader since we have to sort out the names in ascending order from the file                        Names.txt
            */
            BufferedReader d = new BufferedReader(new InputStreamReader(fis));
            String readingFromFile = "";
            String intoArrayList = "";
            while((readingFromFile = d.readLine()) != null)
            {
                /*
                   Now We are using StringTokenizer to read the names from the FileList since  the Names  are separated using comma in the File and the resultant one by one we will store it in the ArrayList which we will sort it out using Collections.sort(list); 

                   Since this class (Orderly) is imported in the class Example it will automatically sort out the List  and we will get the Names in Ascending Order on further iteration of the List
                */
                st = new StringTokenizer(readingFromFile, ",");
                while (st.hasMoreTokens())
                {
                    Orderly orderly=new Orderly();
                    intoArrayList = st.nextToken();
                    orderly.setOrderlyName(intoArrayList);
                    showRecord.add(orderly);
                }
            }
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return showRecord;
    }
}

(c)Names.txt will have the following contents
Seshadri,Lakshmi,Padma,Balaji,Koushick,Hari,Sudarshan,Anirudh,Sriram,Rajagopalan,Mahesh,Zombia,Karthick,Raghavendra,Mahati,Kamran

(d)Now on running the Example.java file we get the following as Result(Sorted Out)
Name is Anirudh
Name is Balaji
Name is Hari
Name is Kamran
Name is Karthick
Name is Koushick
Name is Lakshmi
Name is Mahati
Name is Mahesh
Name is Padma
Name is Raghavendra
Name is Rajagopalan
Name is Seshadri
Name is Sriram
Name is Sudarshan
Name is Zombia