import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; /** * Read a line of input from System.in and convert the * entire line to a String or a number (int or a double). * * Usage: * Keyboard.readLine() ==> next line of user input as a String * Keyboard.readInt() ==> next line of user input as an int * Keyboard.readDouble() ==> next line of user input as a double * * @author Suzanne Menzel */ public class Keyboard { /** Reads and returns the next line of input as a String. */ public static String readLine() { String line = ""; try { line = reader.readLine(); } catch (IOException e) { System.err.println("Error in Keyboard class:" + e); quit(); } return line; } /** Reads and returns the next line of input as an int. */ public static int readInt() { int n = 0; try { n = Integer.parseInt(readLine().trim()); } catch (NumberFormatException e) { System.err.println("Error in Keyboard.readInt()\n" + e); quit(); } return n; } /** Reads and returns the next line of input as a double. */ public static double readDouble() { double n = 0.0; try { n = Double.parseDouble(readLine().trim()); } catch (NumberFormatException e) { System.err.println("Error in Keyboard.readDouble()\n" + e); quit(); } return n; } private static void quit() { throw new RuntimeException("read error"); } private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); }