import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; /** * Class to conveniently handle simple keyboard input. * Does not need to be instantiated, as all methods are static. * Just call Input.getString and Input.getChar as needed. * @author Glenn Matthews */ public class Input { public static BufferedReader input = new BufferedReader( new InputStreamReader( System.in)); /** * Displays the given prompt and reads the user input as a String. * @param prompt String to print before reading the input. * @return The String input by the user. */ public static String getString(String prompt) { System.out.print(prompt); String s; try { s = input.readLine(); } catch (IOException e) { e.printStackTrace(); return null; } return s; } /** * Displays the given prompt and reads the first character of the user * input, converting it to lower case. * @param prompt String to print before reading the input. * @return The first character of the user input, in lower case. */ public static char getChar(String prompt) { String s = getString(prompt); if (s == null || s.length() == 0) { return 0; } return s.toLowerCase().charAt(0); } }