import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; /** InputLab5 - Demo code for Input/Output Console input/output in Java are handled with great caution due to importance on conceptual partitioning of classes, (the class that reads the input returns only one type of data; processing it is someone else's job etc. ) Also due to need for higher security (Java tries to be a more secure language than most others) errors are handled very carefully and explicitly. This makes it incumbent on the programmer to write exception handlers. How to handle "exceptions" will be covered later in the course, but for the purpose of understanding this code, you can look at Horstmann sections 13.1 and 13.3.2. The basic I/O module is explained in Horstmann section 2.8. @author Amitabha Mukerjee @version 3 Sept 2002 */ public class InputLab5 { static InputStreamReader reader = new InputStreamReader(System.in); static BufferedReader console = new BufferedReader(reader); /** Demonstrates the use of the parseInt() method in the Integer class to convert an input text into an integer. THINK: Looking at how it is invoked, can you tell if the method parseInt() a static or non-static method? The variable goofups and how it is used in the NumberFormatException handler is shown merely as a help for you in your lab 5. */ public static int readInt() { boolean done = false; String input; int age=0; int goofups=0; while (!done) { try { System.out.println("What is your age?"); input = console.readLine(); age = Integer.parseInt(input); done = true; } catch (IOException e) { done = true; } catch (NumberFormatException e) { switch (goofups) { case 0: { System.out.println("HEY!! You must type an integer"); break; } case 1: { System.out.println("OK Smartalec. One more chance!! You must type an integer" ); break; } case 2: default: { System.out.println("THAT'S IT!! Now you are eyeball soup!!" ); done = true; break; } } goofups++; } } return age; } /** This is straightforward from Horstmann Section 2.8 */ public static void readInput() { try { System.out.println("What is your name?"); String input = console.readLine(); String name = input; System.out.println("What is your age?"); input = console.readLine(); int age = Integer.parseInt(input); System.out.println("Hello, " + name + ". So you are to die at the meager age of "+ age + "!"); } catch(IOException e) { System.out.println("Input/Output Error" + e); System.exit(1); } catch(NumberFormatException e) { System.out.println("EXITING! Number wasn't an integer!! " + e); System.exit(1); } } /* Demo of how to read one character at a time. You can also read a String directly by readLine as in the above method, readInput(). The read() operation on a Reader object returns an integer version of the unicode character. End of line is indicated as -1 which is checked by the (ch<0) test. */ public static String readChars() { boolean done = false; int ch; String name = ""; String input; System.out.println("What is your name?"); while (!done) { try { ch = console.read(); if ((ch<0) || ( (char)ch == '\n')) done = true; else name += (char)ch; } catch (IOException e) { done = true; } } return name; } }