Advertisement
undefined0000

Line Reader Java

Dec 12th, 2011
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class ReadString {
  4.  
  5.    public static void main (String[] args) {
  6.  
  7.       //  prompt the user to enter their name
  8.       System.out.print("Enter your name: ");
  9.  
  10.       //  open up standard input
  11.       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.       String userName = null;
  14.  
  15.       //  read the username from the command-line; need to use try/catch with the
  16.       //  readLine() method
  17.       try {
  18.          userName = br.readLine();
  19.       } catch (IOException ioe) {
  20.          System.out.println("IO error trying to read your name!");
  21.          System.exit(1);
  22.       }
  23.  
  24.       System.out.println("Thanks for the name, " + userName);
  25.  
  26.    }
  27.  
  28. }  // end of ReadString class
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement