/* Simple input from the keyboard for strings, ints, and doubles in response * to a prompt. Also, a corresponding set of routines for terminal output, * essentially providing alternative names for System.out.print and * System.out.println. * * Input routines first adapted 9/29/01 by Ronald J. Williams (then further modified * 1/7/02 and 3/19/02) from the EasyIn class originally created by Peter van der Linden * 5/5/97 (which can be found on the CD that comes with his "Just Java 2" book). Note that * the methods to be called from user programs are static methods, not instance methods, * so using them does not involve creating instances of the SimpleIO class. * * This is not thread safe, not high performance, and doesn't detect EOF. * It's intended for low-volume easy keyboard input. An example of use is: * * int i = SimpleIO.in.demandInt("Enter an integer:"); // reads an int from System.in * SimpleIO.out.println("Your integer is " + i); // writes to System.out * * Note that any type may be output, but input of only ints, doubles, chars, and Strings is * supported here because this is sufficient for the simple-minded purposes for which * this has been created. Obviously, it would be easy, but tedious, to add code allowing * input of the other 5 primitive types (boolean, byte, short, long, and float) as well. * * Also note that the input routines automatically add a space after the prompt string * because this is what JPT's input routines like console.in.demandInt seem to do. * Furthermore, the names demandXxxx are designed to match the names used in JPT for * functions that behave similarly. */ import java.io.*; import java.util.*; public class SimpleIO { // This allows the user to use SimpleIO.out.print and SimpleIO.out.println // in place of System.out.print and System.out.println, respectively. // It really just assigns another name to System.out. public static PrintStream out = System.out; // This is what enables us to call the input methods using names of the form // SimpleIO.in.demandXxxx. public static SimpleInput in = new SimpleInput(); // This method is here only to demonstrate the use of the simple IO functions. // Although not necessary inside this class, we use the SimpleIO class name as a // a prefix to show how to invoke them from other classes. public static void main (String args[]){ String s = SimpleIO.in.demandString("\nEnter a string:"); SimpleIO.out.print(s); SimpleIO.out.println(); SimpleIO.out.println("Your string is: " + s); int i = SimpleIO.in.demandInt("\nEnter an int:"); SimpleIO.out.println(i); SimpleIO.out.println("Your int is: " + i); double d = SimpleIO.in.demandDouble("\nEnter a double:"); SimpleIO.out.println(d); SimpleIO.out.println("Your double is: " + d); char c = SimpleIO.in.demandChar("\nEnter a char:"); SimpleIO.out.println(c); SimpleIO.out.println("Your char is: " + c); } } // This does all the work involving reading input from the terminal since it // contains the demandInt, demandDouble, demandChar, and demandString methods. class SimpleInput { private static InputStreamReader isr = new InputStreamReader(System.in); private static BufferedReader br = new BufferedReader(isr); private static StringTokenizer st; private static StringTokenizer getToken() throws IOException { String s = br.readLine(); return new StringTokenizer(s); } // Prompts for an int and keeps trying until it gets one public int demandInt(String prompt) { boolean tryAgain = true; int i = 0; while (tryAgain) { try { tryAgain = false; System.out.print(prompt + " "); // note the added space st = getToken(); i = Integer.parseInt(st.nextToken()); } catch (NumberFormatException nfe) { System.err.println("Sorry, you must enter a valid int value."); tryAgain = true; } catch (NoSuchElementException nsee) { System.err.println("Sorry, you must enter a valid int value."); tryAgain = true; } catch (IOException ioe) { System.err.println("IO Exception in SimpleIO.in.demandInt"); } } return i; } // Prompts for a double and keeps trying until it gets one public double demandDouble(String prompt) { boolean tryAgain = true; double d = 0.0; while (tryAgain) { try { tryAgain = false; System.out.print(prompt + " "); // note the added space st = getToken(); d = new Double(st.nextToken()).doubleValue(); } catch (NumberFormatException nfe) { System.err.println("Sorry, you must enter a valid double value."); tryAgain = true; } catch (NoSuchElementException nsee) { System.err.println("Sorry, you must enter a valid double value."); tryAgain = true; } catch (IOException ioe) { System.err.println("IO Exception in SimpleIO.in.demandDouble"); } } return d; } // Prompts for a char and keeps trying until it gets one // Only returns the first char entered if more than one public char demandChar(String prompt) { boolean tryAgain = true; String s = ""; while (tryAgain) { try { System.out.print(prompt + " "); // note the added space s = br.readLine(); if (s.length() > 0) tryAgain = false; else System.err.println("Sorry, you must enter a valid character."); } catch (IOException ioe) { System.err.println("IO Exception in SimpleIO.in.demandChar"); } } return s.charAt(0); } // Prompts for a string public String demandString(String prompt) { try { System.out.print(prompt + " "); // note the added space return br.readLine(); } catch (IOException ioe) { System.err.println("IO Exception in SimpleIO.in.demandString"); return ""; } } }