import java.io.*; import java.util.*; /* Program for simple input from keyboard and files. B. A. Sanders, July, 1997, modified Dec. 1997 Modified by B. L. Massingill, Feb. 1998, to add setStdin() method main at bottom illustrates use. All routines throw an InputEOFException if an end of file is encountered. IOexceptions have been converted to InputIOExceptions InputEOFExceptions and InputIOExceptions are both RuntimeExceptions that need not be declared in clients. Input entry is flexible but if an illegal value is encountered, the remaining entries on the same line are lost. To read from a file instead of System.in, use setFile to change the reader to a file. To switch back to reading from System.in, use setStdin. This implementation avoids use of BufferedReader since the windows implementation seems to have problems. */ class InputEOFException extends InputIOException { InputEOFException(String s) { super(s); } InputEOFException() { } } class InputIOException extends RuntimeException { InputIOException(String s) { super(s); } InputIOException() { } } class Input { static StringTokenizer st; static Reader br = new InputStreamReader(System.in); public static void setFile(String file) { try{ br = new FileReader(file); } catch(FileNotFoundException e) { throw new InputIOException("File " + file + " not found"); } } public static void setStdin() { br = new InputStreamReader(System.in); } private static String readLine()throws IOException { StringBuffer sb = new StringBuffer(); int ich = br.read(); char ch = (char)ich; if(ich == -1) return null; else //at least one character has been read while ( ch!='\n' && ch!='\r' && ich != -1) { sb.append(ch); ich = br.read(); ch = (char)ich; } return sb.toString(); } private static String nextToken() { try { int test0 = 0; while (st == null || !st.hasMoreTokens()) { String s = readLine(); if (s == null) throw(new InputEOFException("Encountered EOF")); st = new StringTokenizer(s); } return st.nextToken(); }catch (IOException e) { throw( new InputIOException()); } } private static void flushLine(){ st = null; } public static int readInt() { while(true) { try { return Integer.parseInt(nextToken()); } catch (NumberFormatException e) { flushLine(); System.out.println("Input not an integer, try again"); } } } public static long readLong() { while(true) { try { return Long.parseLong(nextToken()); } catch (NumberFormatException e) { flushLine(); System.out.println("Input not an integer, try again"); } } } public static boolean readBoolean() { String s; while(true) { s = readString(); if (s.equals("true")) return true; else if(s.equals("false")) return false; else { flushLine(); System.out.println("Input not a boolean value, try again"); } } } public static String readString() { return nextToken(); } public static float readFloat() { while(true) { try { return new Float(nextToken()).floatValue(); } catch (NumberFormatException e) { flushLine(); System.out.println("Input not a float, try again"); } } } public static double readDouble() { while(true) { try { return new Double(nextToken()).doubleValue(); } catch (NumberFormatException e) { flushLine(); System.out.println("Input not a double, try again"); } } } public static void main(String[] args) throws IOException { System.out.println("Testing modified version"); System.out.println("Enter two integers"); int i = readInt(); long j= readLong(); System.out.println("You entered " + i + " and " + j); System.out.println("Enter a boolean"); boolean b = readBoolean(); System.out.println("You entered " + b); System.out.println("Enter two real numbers"); float fl = readFloat(); double d = readDouble(); System.out.println("You entered " + fl + " and " + d); System.out.println("Enter a string"); String str = readString(); System.out.println("You entered " + str); System.out.println("Enter file"); String f = readString(); setFile(f); while(true) { System.out.println(readString()); } } }