// Simple input from the keyboard for all primitive types. ver 1.0 // Copyright (c) Peter van der Linden, May 5 1997. // corrected error message 11/21/97 // // The creator of this software hereby gives you permission to: // 1. copy the work without changing it. // 2. modify the work providing you send me a copy which I can // use in any way I want, including incorporating into this work. // 3. distribute copies of the work to the public by sale, lease, // rental, or lending. // 4. perform the work. // 5. display the work. // 6. fold the work into a funny hat and wear it on your head. // // This is not thread safe, not high performance, and doesn't tell EOF. // It's intended for low-volume easy keyboard input. // An example of use is: // EasyIn easy = new EasyIn(); // int i = easy.readInt(); // reads an int from System.in // float f = easy.readFloat(); // reads a float from System.in /* Modified heavily by Frederic Briere for use in the MiniJav compiler. Better error-handling was a priority. Daniel Côté 2006-06-07: Make all static and avoid having to create a field in the client class. New usage in client is now: scanf.readI(); scanf.readF(); ... Added other methods for all Java primitive types: scanf.readS() for short integers, scanf.readB() for bytes, scanf.readC() for characters, scanf.readJ() for long integers, scanf.readZ() for booleans, scanf.readD() for double precision floating point numbers. */ package mjio; import java.io.*; import java.util.*; public class scanf { /* Reading from stdin */ private static BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); /* This holds a StringTokenizer for the current line */ private static StringTokenizer st = null; /* Flag to report end of file */ private static boolean isEOF = false; /*************************************************************************** ** Fetch a line if necessary */ private static void fetchLine() throws IOException { String s = br.readLine(); if ( s == null ) { isEOF = true; st = null; System.err.println( "Reading beyond EOF" ); throw new EOFException(); } st = new StringTokenizer( s ); } /*************************************************************************** ** Get the next token, fetching lines if necessary */ private static String getToken() throws IOException { String token = null; /* Although unnecessary, this avoids system calls after EOF */ if ( isEOF ) throw new EOFException(); if ( st == null ) fetchLine(); while ( token == null ) { try { token = st.nextToken(); } catch ( NoSuchElementException e ) { fetchLine(); } } return( token ); } /*************************************************************************** ** Read a short integer * return 0 on any error. */ public static short readS() { short value = 0; try { value = Short.parseShort( getToken() ); } catch ( NumberFormatException e ) { System.err.println( "Badly formatted short integer on input." ); } catch ( EOFException e ) { } catch ( IOException e ) { System.err.println( "IO Exception in scanf/readS()S." ); } return value; } /*************************************************************************** ** Read an integer * return 0 on any error. */ public static int readI() { int value = 0; try { value = Integer.parseInt( getToken() ); } catch ( NumberFormatException e ) { System.err.println( "Badly formatted integer on input." ); } catch ( EOFException e ) { } catch ( IOException e ) { System.err.println( "IO Exception in scanf/readI()I." ); } return value; } /*************************************************************************** ** Read a long integer * return 0 on any error. */ public static long readJ() { long value = 0L; try { value = Long.parseLong( getToken() ); } catch ( NumberFormatException e ) { System.err.println( "Badly formatted long integer on input." ); } catch ( EOFException e ) { } catch ( IOException e ) { System.err.println( "IO Exception in scanf/readJ()J." ); } return value; } /*************************************************************************** ** Read a byte * return 0 on any error. */ public static byte readB() { byte value = 0; try { value = Byte.parseByte( getToken() ); } catch ( NumberFormatException e ) { System.err.println( "Badly formatted byte on input." ); } catch ( EOFException e ) { } catch ( IOException e ) { System.err.println( "IO Exception in scanf/readB()B." ); } return value; } /*************************************************************************** ** Read a char * return 0 on any error. */ public static char readC() { char value = 0; try { String tk = getToken(); value = ( tk == null || tk.length() == 0 ) ? 0 : tk.charAt( 0 ); } catch ( EOFException e ) { } catch ( IOException e ) { System.err.println( "IO Exception in scanf/readC()C." ); } return value; } /*************************************************************************** ** Read a boolean * return false on any error. */ public static boolean readZ() { boolean value = false; try { value = ( new Boolean( getToken() ) ).booleanValue(); } catch ( EOFException e ) { } catch ( IOException e ) { System.err.println( "IO Exception in scanf/readZ()Z." ); } return value; } /*************************************************************************** ** Read a floating point number * return 0.0F on any error. */ public static float readF() { float value = 0.0F; try { value = Float.parseFloat( getToken() ); } catch ( NumberFormatException e ) { System.err.println( "Badly formatted floating-point on input." ); } catch (EOFException e) { } catch (IOException e) { System.err.println( "IO Exception in scanf/readF()F." ); } return value; } /*************************************************************************** ** Read a double precision floating point number ** return 0.0D on any error. */ public static double readD() { double value = 0.0D; try { value = Double.parseDouble( getToken() ); } catch ( NumberFormatException e ) { System.err.println( "Badly formatted double precision`" ); System.err.println( "floating-point on input." ); } catch (EOFException e) { } catch (IOException e) { System.err.println( "IO Exception in scanf/readD()D." ); } return value; } }