Showing posts with label JAVASTRINGS. Show all posts
Showing posts with label JAVASTRINGS. Show all posts

Wednesday, 8 February 2017

PROGRAM TO WRITE AND READ DATA FROM .CSV FILE IN JAVA

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Read1 { 
          public static void main(String[] args) throws IOException{
                   BufferedWriter bw = new BufferedWriter(new FileWriter("excel.csv"));
                   bw.write("101,Rakesh,MCA");
                   bw.newLine();
                   bw.write("102,Bikash,MCA");
                   bw.newLine();
                   bw.write("101,Harry,Btech");
                   bw.flush();
                   bw.close();
                   System.out.println("RollNo\tName\tBranch");
                   BufferedReader br = new BufferedReader(new FileReader("excel.csv"));
                   String line;
                   try{                              
                   while((line=br.readLine())!=null)
                   {
                             String lines[] = line.split(",");
                             System.out.println(lines[0]+"\t"+lines[1]+"\t"+lines[2]);                                               
                   }
                   }
                   catch(Exception e){}
                   br.close();           
          }
}

Output –
RollNo        Name                   Branch
101              Rakesh                 MCA
102             Bikash                  MCA
101              Harry                   Btech

Thursday, 2 February 2017

STRING SPLITTING FROM CSV (COMMA SEPARATED VALUES) IN JAVA

This concept is mostly used to extract data from .csv file which is comma separated file. Here we can specify the delimeter comma to be separated from the string and can display it in exact format.

Example –
public class Tokenizer {
          public static void main(String[] args) {
                   String csv = "Smith,Harry,Allen,Bob,Mike";
                   String name[] = csv.split(",");
                   for(String names : name)
                   {
                             System.out.println(names);
                   }
                   System.out.println();
                   System.out.println(name[3]);
          }
}

Output –
Smith
Harry
Allen
Bob
Mike
Bob 

STRING TOKENIZER IN JAVA

The string tokenizer class allows an application to split strings into tokens. The tokenization method is musch simpler than one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.


The default delimiters are –

1. The space character.
2. The tab character (\t)
3. The new line character (\n)
4. The carriage-return character (\r)
5. The form-feed character (\f)

From version 1.4 onwards it is recommended to use string class “split” method rather than StringTokenizer class.


Constructors of StringTokenizer class –

1. public StringTokenizer(String str) – It breaks the given string with above default delimiters.
2. public StringTokenizer (String str, String delim) – It breaks the given string with given delimiter.
3. public StringTokenizer(String str, String delim, Boolean returndelims) – It breaks the given string with given delimiter and also includes delimiter as token.


Methods of StringTokenizer-

1. public int countToken() – To get token count.
2. public boolean hasMoreElements() – To check is token available or not.
3. public boolean hasNextToken() – To check is token available or not.
4. public Object nextElement() – To retrieve token
5. public String nextToken() - To retrieve token


Example –
import java.util.StringTokenizer;
public class Tokenizer {
          public static void main(String[] args) {
                   StringTokenizer st = new StringTokenizer("I love coding in java");
                   System.out.println(st.countTokens());
                   while(st.hasMoreTokens())
                   {
                             System.out.println(st.nextToken());
                   }
                   System.out.println();
                   StringTokenizer st1 = new StringTokenizer("i love coding in java", "o");
                   while(st1.hasMoreTokens())
                   {
                             System.out.println(st1.nextToken());
                   }
                   System.out.println();
                   StringTokenizer st2 = new StringTokenizer("i love coding in java", "o", true);
                   System.out.println(st2.countTokens());
                   System.out.println();
                   while(st2.hasMoreTokens())
                   {
                             System.out.println(st2.nextToken());
                   }
                   System.out.println();
                   StringTokenizer st3 = new StringTokenizer("i love coding in java", "o", false);
                   System.out.println(st3.countTokens());
                   System.out.println();
                   while(st3.hasMoreTokens())
                   {
                             System.out.println(st3.nextToken());
                   }
          }
}

Output –
5
I
love
coding
in
java

i l
ve c
ding in java

5

i l
o
ve c
o
ding in java

3

i l
ve c
ding in java 

toString() METHOD IN JAVA

To represent any object in the form of string, we use toString() method. Suppose if we print the object first then it will generate the hashcode of that particular object. So to generate user specific code we need to override toString() method. Using toString() method we can return the value of the object and we don’t have to write long code to fetch its values.

Example –
public class Reuse {
          String name;
          int rollno; 
          public Reuse(String name, int rollno) {
                   super();
                   this.name = name;
                   this.rollno = rollno;
          }
          public static void main(String[] args) {
                   Reuse r1 = new Reuse("abc", 101);
                   Reuse r2 = new Reuse("xyz", 111);
                   System.out.println(r1);
                   System.out.println(r2);                
          }
}

Output –
string.Reuse@15db9742
string.Reuse@6d06d69c

Example –
public class Reuse {
          String name;
          int rollno; 
          public Reuse(String name, int rollno) {
                   super();
                   this.name = name;
                   this.rollno = rollno;
          }       
          @Override
          public String toString() {
                   return "name=" + name + ", rollno=" + rollno;
          }
          public static void main(String[] args) {
                   Reuse r1 = new Reuse("abc", 101);
                   Reuse r2 = new Reuse("xyz", 111);
                   System.out.println(r1);
                   System.out.println(r2);                
          }
}

Output –
name=abc, rollno=101
name=xyz, rollno=111


Note – After overriding the toString() method we can the user defined output.