/* StandardInput.java */ import java.io.*; /* * This class does some simple parsed input/output. * * A "word" is a string of non-blank text (e.g., in the sentence * "we live as we dream," there are 5 words). */ public class StandardInput { /** Construct a StandardInput object ready for getting parsed input. * @exception IOException if there are any IO problems. */ public StandardInput() throws IOException { Reader rInput = new BufferedReader(new InputStreamReader( System.in )); stInput = new StreamTokenizer( rInput ); } /** Checks to see if the end of file has been reached. * @return true <==> no more input waiting */ public boolean isEOF() { return (stInput.ttype == StreamTokenizer.TT_EOF); } /** Returns the next "word" from the input stream. A * a word is a string of characters that does not contain * any white spaces (blanks, tabs, etc.). The longest * possible word is read from the input and returned, not * including any whitespace at the beginning or end. * @exception IOException if there are any IO problems. */ public String readWord() throws IOException { stInput.nextToken(); return stInput.sval; } /** Returns the next token in the input stream as an integer. * @return the int value of the next input * @exception IOException if the next input is not an integer * or if there are any IO problems. */ public int readInt() throws IOException { stInput.nextToken(); if( stInput.ttype != StreamTokenizer.TT_NUMBER ) { throw new IOException(); } return (int)stInput.nval; } /** Returns the next token in the input stream as a double. * @return the double value of the next input * @exception IOException if the next input is not a double * or if there are any IO problems. */ public double readDouble() throws IOException { stInput.nextToken(); if( stInput.ttype != StreamTokenizer.TT_NUMBER ) { throw new IOException(); } return stInput.nval; } /** Returns the next character in the input stream, not including * any whitespace. * @exception IOException if there are any IO problems. */ public char readByte() throws IOException { char c; do { c = (char)System.in.read(); } while( isWhitespace(c) ); return c; } /** Checks to see if c is a whitespace character. * @param c is the character to be checked. */ private static boolean isWhitespace( char c ) { return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'); } private StreamTokenizer stInput; }