// File: screen_io3.java // // Author; Rahul Simha // Created: Aug 17, 1998 // // Another example in screen I/O import java.io.*; public class screen_io3 { // Read in a string after putting out a prompt. public static String read_string (String prompt) { OutputStreamWriter osw = new OutputStreamWriter (System.out); BufferedWriter bw = new BufferedWriter (osw); try { bw.write (prompt); bw.flush (); // Need to flush buffer to ensure output } catch (IOException e) { System.out.println ("new_io::read_string_prompt1: cannot write\n"); System.exit (1); return ""; } InputStreamReader isr = new InputStreamReader (System.in); LineNumberReader lr = new LineNumberReader (isr); try { String s = lr.readLine (); return s; } catch (IOException e) { System.out.println ("new_io::read_string_prompt1: cannot read\n"); System.exit (1); return ""; } } // Read in an int. public static int read_int (String prompt) { String s = read_string (prompt); while (true) { try { int i = Integer.parseInt (s); return i; } catch (NumberFormatException e) { s = read_string (prompt); } } } public static void main (String[] argv) { // Example: int i = read_int ("Enter an int: "); System.out.println ("What you entered: " + i); } }