home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch02 / Input.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  1.2 KB  |  45 lines

  1. import java.io.*;
  2.  
  3. /*
  4.  * Input.class reads a line of text from standard input,
  5.  * determines the length of the line, and prints it.
  6.  */
  7.  
  8. class Input {
  9. public static void main (String args[]) {
  10.        String input = "";
  11.        boolean error;
  12.  
  13. /*
  14.  * BufferedReader contains the readLine method.
  15.  * Create a new instance for standard input System.in
  16.  */
  17. BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
  18. /*
  19.  * This loop is used to catch I/O exceptions that may occur
  20.  */
  21.       do {
  22.              error = false;
  23.              System.out.print ("Enter the string > ");
  24.  
  25. /*
  26.  * We need to flush the output - no newline at the end
  27. */
  28.              System.out.flush ();
  29.  
  30.              try {
  31.                   input = in.readLine ();       
  32. } catch (IOException e) {
  33.                   System.out.println (e);       
  34. System.out.println ("An input error was caught");
  35. error = true;
  36.              }
  37.       } while (error);
  38.        
  39.       System.out.print ("You entered \"");
  40.       System.out.print (input);      /
  41. System.out.println ("\"");
  42.       System.out.print ("The length is ");
  43.       System.out.println (input.length ());     
  44. } // end of main ()
  45.