Reading Strings using java console program


To read a string from the keyword ,use the version of readLine() that is a member of the BufferedReader class .
Java Need Some headefile

import java.io.*;

Its general from is shown here :

String readLine() throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

As you can see ,it returns String object.
The following program demostrates BufferedReader and the readline() method the program reads and displayes lines of text until you enter the word "stop" .

Program :edit.java

import java.io.*;
class edit
{
   public static void main(String arg[]) throws IOException
    {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     String str[]=new String[100];
     System.out.println("Enter lines of text :");
     System.out.println("Enter 'stop' to quit.");
     for(int i=0;i<100;i++)
      {
        str[i]=br.readLine();
        if(str[i].equals("stop"))break;
      }
     System.out.println("\nHere is your file:");
     for(int i=0;i<100;i++)
      {
        if(str[i].equals("stop"))break;
        System.out.println(str[i]);
      }
     }
}
Output 


G:\>javac edit.java

G:\>java edit
Enter lines of text :
Enter 'stop' to quit.
hai
how
are
you
stop

Here is your file:
hai
how
are
you

Reading Strings using java

0 comments:

Post a Comment

 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org