// I need this library import java.io.*; // class that knows weird (but helpful) incantations class CIS214 { // method to read input from a user // input is returned as a string public static String getInput(String prompt) { String user = null; // input is dangerous try { System.out.print(prompt+" > "); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); // actual read is here user = in.readLine(); } catch (IOException e) { System.out.println("IOException " + e); } return user; } // method to get a number from a user // keeps asking until valid int given public static int getIntInput(String prompt) { // keep asking forever, return value ends loop while (true) { // in case the user is stupid try { int p = Integer.parseInt(getInput(prompt)); return p; // loop ends } catch (Exception e) { System.out.println("Input invalid. Try again."); } } } // method to get a number from a user // keeps asking until valid int given public static double getDoubleInput(String prompt) { // keep asking forever, return value ends loop while (true) { // in case the user is stupid try { double p = Double.parseDouble(getInput(prompt)); return p; // loop ends } catch (Exception e) { System.out.println("Input invalid. Try again."); } } } // testing public static void main(String [] args) { String s = getInput("request a string"); double d = getDoubleInput("request a double"); int i = getIntInput("request an int"); System.out.println("s = " + s); System.out.println("d = " + d); System.out.println("i = " + i); } }