package io; import java.io.*; /** * Gives basic support for reading an integer, a double or a * string from the terminal(stdin). * * Methods read one item per line. * * @version 1.2.1, Tues May 19 1998 * @author Rolf W. Rasmussen * @author Khalid A. Mughal * @author Michal Walicki */ public final class Stdio { private Stdio() {}; private static BufferedReader reader = new BufferedReader( new InputStreamReader(System.in) ); /** Print out a string or the text representation of any other object to the terminal. */ public static void prompt( Object str) { System.out.print(str); System.out.flush(); } /** Print out a string or the text representation of any other object to the terminal and skip to the next line. */ public static void promptln( Object str ) { System.out.println( str ); } public static void prompt(int n) { prompt(new Integer(n)); } public static void promptln(int n) { promptln(new Integer(n)); } /** * Skip to next line on the terminal. */ public static void promptln() { System.out.println( "" ); } /** * Read one line of text from the terminal and return it as a string. */ public static String getString() { String str; while (true) try { return reader.readLine(); } catch(IOException io) { reportError(); } } /** * Read one integer value from the terminal. */ public static int readInteger() { while (true) try { return Integer.parseInt( reader.readLine() ); } catch (IOException io) { reportError(); } catch(NumberFormatException nf) { reportError(); } } /** * Read one double value from the terminal. */ public static double readDouble() { while (true) try { return Double.valueOf( reader.readLine() ).doubleValue(); } catch(IOException io) { reportError(); } catch(NumberFormatException nf) { reportError(); } } /** * Read a single 8-bit character from the terminal. */ public static char read8BitChar() { while (true) try { return (char) reader.read(); } catch(IOException io) { reportError(); } } private static void reportError() { promptln( "Error in input. Re-enter data." ); } }