/* Input class////////////////////////////////Input.java A halfhearted attempt to provide some reasonable console keyboard input to Java (c) Steven C. Lawlor, 1997, 1999 May be used for Foothill's Java classes. All other rights reserved. */ /*******************************************************Input class line() Returns an input line from the current stream position word() Returns a single word from the current stream position Whitespace delimited. Ignores leading and trailing whitespace. character() Returns a single character from the current stream position. Whitespace characters are accepted, not ignored. Often must flush() beforehan integer() Returns an integer (int) from the current stream position. Whitespace delimited. Ignores leading and trailing whitespace. An invalid integer prints an error message and returns zero. real() Returns a double from the current stream position. Whitespace delimited. Ignores leading and trailing whitespace. An invalid double prints an error message and returns zero. flush() Empties the input stream. Input.class should reside in the same directory as the source code unless CLASSPATH is set, then it should be in that path. Usage: int i; i = Input.integer(); The main() method is provided to test the class. */ public class Input { public static String line() //*******************Accepts Entire Line { char c; String str = ""; boolean fin = false; boolean leading = true; while(!fin) { try { c = (char)System.in.read(); if(Character.isWhitespace(c)) { if(!leading) { if( c== '\n') fin = true; else str = str + c; } } else { leading = false; str =str + c; } } catch(java.io.IOException e) { fin = true; } } return str.trim(); } public static String word() //*****************Accepts Next Word { char c; String str = ""; boolean fin = false; boolean leading = true; while( !fin) { try { c = (char)System.in.read(); if(Character.isWhitespace(c)) { if( !leading) fin = true; } else { leading = false; str = str + c; } } catch(java.io.IOException e) { fin = true; } } return str.trim(); } public static void flush() //***************Empties Input Stream { try { while(System.in.available() > 0) System.in.read(); } catch(java.io.IOException e) { System.out.println("Error inputting character."); } } public static char character() //*********Accepts Next Character { char c; try { c = (char)System.in.read(); } catch(java.io.IOException e) { c = ' '; } return c; } public static int integer() //**********Process Next Number { String str; str = word(); try { return Integer.valueOf(str).intValue(); } catch(NumberFormatException e) { System.out.println(str + " an invalid integer."); } return 0; } public static double real() //**********Process Next Number { String str; str = word(); try { return Double.valueOf(str).doubleValue(); } catch(NumberFormatException e) { System.out.println(str + "an invalid real number."); } return 0; } public static void main(String[] args) //*****Tests the Class { String s; char c; int i; double r; System.out.print("A line>"); s = Input.line(); System.out.println("Line: " + s); System.out.print("Two words>"); s = Input.word(); System.out.println("First word: " + s); s = Input.word(); System.out.println("Second word: " + s); Input.flush(); System.out.print("Just one character>"); c = Input.character(); System.out.println("Character: " + c); System.out.print("Integer>"); i = Input.integer(); System.out.println("Integer: " + s); System.out.print("Real>"); r = Input.real(); System.out.println("Real: " + r); System.out.print("A word, an integer, and a real>"); s = Input.word(); System.out.println("Word: " + s); i = Input.integer(); System.out.println("Integer: " + i); r = Input.real(); System.out.println("Real: " + r); } }