/* * RedmondMsgIn.java * * Created on August 14, 2002, 12:56 PM * Copy of version created on April 17, 2002, 10:33 PM * (just put inside I/O package now) */ package IO; import javax.swing.*; /** * * @author mike */ public class RedmondMsgIn { /** Creates a new instance of RedmondMsgIn */ public RedmondMsgIn() { } /** * Use msg boxes to ask user for a valid integer * Continues asking until a valid integer is entered * Does not handle totally bad input (e.g. entering a letter or a decimal number) * * Given: * message to display to the user * lowest valid response * highest valid response * Returns: * the valid integer entered */ public static int readValidInt(String message, int min, int max) { // fix bad request if (min > max) { // swap them int temp = min; min = max; max = temp; } String answerString; // ask for input and get output answerString = JOptionPane.showInputDialog(message); int answer; // convert output to integer // this will bomb if user enters a char or a double - watch out answer = Integer.parseInt(answerString); // ensure valid input - don't let go on without valid entry while ((answer < min) || (answer > max)) { // ask for input and get output answerString = JOptionPane.showInputDialog(message + " (between " + min + " and " + max + ")"); // convert output to integer // this will bomb if user enters a char or a double - watch out answer = Integer.parseInt(answerString); } // if we get here - we have a valid input - return it return answer; } /** * Use msg boxes to ask user for a valid integer * Continues asking until a valid integer is entered * Robust version - but don't show code until exception handling covered * Handles totally bad input (e.g. entering a letter or a decimal number) * * Given: * message to display to the user * lowest valid response * highest valid response * Returns: * the valid integer entered */ public static int readValidIntRobust(String message, int min, int max) throws UserCancelException { // fix bad request if (min > max) { // swap them int temp = min; min = max; max = temp; } String answerString = ""; // ask for input and get output boolean done = false; int answer = -9; // keep on trying until a good valid value is found while (! done) { try { // ask for input and get answer answerString = JOptionPane.showInputDialog(message); answerString = answerString.trim(); // convert output to integer // this will bomb if user enters a char or a double // - and will be caught below answer = Integer.parseInt(answerString); // see if invalid answer if ((answer < min) || (answer > max)) { // make msg more specific message = message + " (between " + min + " and " + max + ")"; // lack of changing done will cause reprompt - with adjusted message } else { // good answer - get out of try catch so can return answer done = true; } } // end try // catch bad input - char or decimal catch (NumberFormatException e) { // display complaints - then will loop back and ask again System.out.println( "Your input number is not correct."); System.out.println("Your input number must be a whole number written as an"); System.out.println("ordinary numeral, such as 42. Instead you typed " + answerString); System.out.println("Minus signs are OK," + "but do not use a plus sign."); System.out.println("Please, try again. Enter a whole number this time!"); } // catch user cancel - avoid going kablooey in my code catch (NullPointerException e) { // throw out UserCancelException throw new UserCancelException("User cancelled", "readValidIntRobust", message, min, max); } } // end while return answer; } // end method /** * Use msg boxes to ask user for a valid long integer * Continues asking until a valid long integer is entered * Robust version - but don't show code until exception handling covered * Handles totally bad input (e.g. entering a letter or a decimal number) * * Given: * message to display to the user * lowest valid response * highest valid response * Returns: * the valid integer entered */ public static long readValidLongRobust(String message, long min, long max) throws UserCancelException { // fix bad request if (min > max) { // swap them long temp = min; min = max; max = temp; } String answerString = ""; // ask for input and get output boolean done = false; long answer = -9; // keep on trying until a good valid value is found while (! done) { try { // ask for input and get answer answerString = JOptionPane.showInputDialog(message); answerString = answerString.trim(); // convert output to integer // this will bomb if user enters a char or a double // - and will be caught below answer = Long.parseLong(answerString); // see if invalid answer if ((answer < min) || (answer > max)) { // make msg more specific message = message + " (between " + min + " and " + max + ")"; // lack of changing done will cause reprompt - with adjusted message } else { // good answer - get out of try catch so can return answer done = true; } } // end try // catch bad input - char or decimal catch (NumberFormatException e) { // display complaints - then will loop back and ask again System.out.println( "Your input number is not correct."); System.out.println("Your input number must be a whole number written as an"); System.out.println("ordinary numeral, such as 42. Instead you typed " + answerString); System.out.println("Minus signs are OK," + "but do not use a plus sign."); System.out.println("Please, try again. Enter a whole number this time!"); } // catch user cancel - avoid going kablooey in my code catch (NullPointerException e) { // throw out UserCancelException throw new UserCancelException("User cancelled", "readValidLongRobust", message, min, max); } } // end while return answer; } // end method /** * Use msg boxes to ask user for a valid double * Continues asking until a valid double is entered * Robust version - but don't show code until exception handling covered * Handles totally bad input (e.g. entering a letter or a decimal number) * * Given: * message to display to the user * lowest valid response * highest valid response * Returns: * the valid double entered */ public static double readValidDoubleRobust(String message, double min, double max) throws UserCancelException { // fix bad request if (min > max) { // swap them double temp = min; min = max; max = temp; } String answerString = ""; // ask for input and get output boolean done = false; double answer = -9; // keep on trying until a good valid value is found while (! done) { try { // ask for input and get answer answerString = JOptionPane.showInputDialog(message); answerString = answerString.trim(); // convert output to integer // this will bomb if user enters a char or a double // - and will be caught below answer = Double.parseDouble(answerString); // see if invalid answer if ((answer < min) || (answer > max)) { // make msg more specific message = message + " (between " + min + " and " + max + ")"; // lack of changing done will cause reprompt - with adjusted message } else { // good answer - get out of try catch so can return answer done = true; } } // end try // catch bad input - char or catch (NumberFormatException e) { // display complaints - then will loop back and ask again System.out.println( "Your input number is not correct."); System.out.println("Your input number must be a decimal number written as an"); System.out.println("ordinary numeral, such as 42.5. Instead you typed " + answerString); System.out.println("Minus signs are OK," + "but do not use a plus sign."); System.out.println("Please, try again. Enter a number this time!"); } // catch user cancel - avoid going kablooey in my code catch (NullPointerException e) { // throw out UserCancelException throw new UserCancelException("User cancelled", "readValidDoubleRobust", message, min, max); } } // end while return answer; } // end method /** * Use msg boxes to ask user for a valid integer * Continues asking until a valid integer is entered * Does not handle totally bad input (e.g. entering a letter or a decimal number) * * Made available as an easier name than readValidInt * * Given: * message to display to the user * lowest valid response * highest valid response * Returns: * the valid integer entered */ public static int readValid(String message, int min, int max) { int answer; answer = RedmondMsgIn.readValidInt(message, min, max); return answer; } /** * Use msg boxes to ask user for a valid double * Continues asking until a valid double is entered * Does not handle totally bad input (e.g. entering a letter or nothing) * * Made available as a different name than readValid * * Given: * message to display to the user * lowest valid response * highest valid response * Returns: * the valid double entered */ public static double readValidDouble(String message, double min, double max) { double answer; answer = RedmondMsgIn.readValid(message, min, max); return answer; } /** * Use msg boxes to ask user for a valid double * Continues asking until a valid double is entered * Does not handle totally bad input (e.g. entering a letter) * * Given: * message to display to the user * lowest valid response * highest valid response * Returns: * the valid integer entered */ public static double readValid(String message, double min, double max) { // fix bad request if (min > max) { // swap them double temp = min; min = max; max = temp; } String answerString; // ask for input and get output answerString = JOptionPane.showInputDialog(message); double answer; // convert output to double // this will bomb if user enters a char - watch out answer = Double.parseDouble(answerString); // ensure valid input - don't let go on without valid entry while ((answer < min) || (answer > max)) { // ask for input and get output answerString = JOptionPane.showInputDialog(message + " (between " + min + " and " + max + ")"); // convert output to double // this will bomb if user enters a char - watch out answer = Double.parseDouble(answerString); } // if we get here - we have a valid input - return it return answer; } /** * Use msg boxes to ask user for a valid Y/N answer * Continues asking until a valid answer is entered * Robust version - but don't show code until exception handling covered * Handles user canceling * * Given: * message to display to the user * Returns: * the valid answer entered - always in upper case */ public static char readValidYNRobust(String message) throws UserCancelException { // fix bad request String answerString = ""; // ask for input and get output boolean done = false; char answer = 'Y'; // keep on trying until a good valid value is found while (! done) { try { // ask for input and get output answerString = JOptionPane.showInputDialog(message); // take only the first char // this will cause an exception if user cancels // this will not work well if user enters leading space(s) answer = answerString.charAt(0); // convert output to upper case // this will bomb if user enters a char or a double // - and will be caught below answer = Character.toUpperCase(answer); // see if invalid answer // ensure valid input - don't let go on without valid entry if ((answer == 'N') || (answer == 'Y')) { done = true; } else { message = message + " (must be y or n) "; } } // end try // catch bad input - number or - i don't know if this applies here /* catch (NumberFormatException e) { // display complaints - then will loop back and ask again System.out.println( "Your input number is not correct."); System.out.println("Your input number must be a Y or N"); System.out.println("Please, try again. Enter a letter this time!"); } **/ // catch user cancel - avoid going kablooey in my code catch (NullPointerException e) { // throw out UserCancelException throw new UserCancelException("User cancelled", "readValidYNRobust", message); } } // end while // if we get here - we have a valid input - return it // always return upper case so more predictable for caller answer = Character.toUpperCase(answer); // prob redundant now return answer; } // end method /** * Use msg boxes to ask user for a valid Yes or No answer * Continues asking until a valid answer is entered * Accepts 'y', 'Y', 'n', 'N', "yes", "no" etc * * Given: * message to display to the user * Returns: * the valid answer entered */ public static char readValidYN(String message) { String answerString; // ask for input and get output answerString = JOptionPane.showInputDialog(message); char answer; // take only the first char // this will not work well if user enters leading space(s) answer = answerString.charAt(0); // ensure valid input - don't let go on without valid entry while ((answer != 'n') && (answer != 'N') && (answer != 'y') && (answer != 'Y')) { // ask for input and get output answerString = JOptionPane.showInputDialog(message + " (must be y or n) "); // take only the first char // this will not work well if user enters leading space(s) answer = answerString.charAt(0); } // if we get here - we have a valid input - return it // always return upper case so more predictable for caller answer = Character.toUpperCase(answer); return answer; } /** * Use msg boxes to ask user for a valid string answer * Strings are not validated here - accepts any string * * Given: * message to display to the user * Returns: * the answer entered */ public static String readString(String message) { String answerString; // ask for input and get output answerString = JOptionPane.showInputDialog(message); if (answerString == null) { return ""; } else { // trim extra white space answerString = answerString.trim(); return answerString; } } /** * Use msg boxes to ask user for a string answer * allows empty string * Robust version - but don't show code until exception handling covered * Handles user canceling * * Given: * message to display to the user * Returns: * the valid answer entered */ public static String readStringRobust(String message) throws UserCancelException { // fix bad request String answerString = ""; // ask for input and get output answerString = JOptionPane.showInputDialog(message); if (answerString == null) { // catch user cancel - let user know // throw out UserCancelException throw new UserCancelException("User cancelled", "readStringRobust", message); } // if we get here - we have a valid input - return it return answerString; } // end method /** * @param args the command line arguments * little test driver */ public static void main (String args[]) { try { char yn = RedmondMsgIn.readValidYNRobust("Do you want to play hearts?"); System.out.println("echo: " + yn); } catch (UserCancelException e) { // not much to do in this case System.out.println("You didn't answer the question"); System.out.println(e); // use the exception class's toString method } //String answ = RedmondMsgIn.readString("What is your pin?"); //System.out.println("PIN: " + answ); try { String answ = RedmondMsgIn.readStringRobust("What is your pin?"); System.out.println("PIN: " + answ); } catch (UserCancelException e) { // not much to do in this case System.out.println("You didn't answer my question - I must have your PIN"); System.out.println(e); // use the exception class's toString method } try { int age = RedmondMsgIn.readValidIntRobust("Please enter your age",0,120); System.out.println("Your age is: " + age); } // catch user cancelled catch (UserCancelException e) { // not much to do in this case System.out.println("You refused to give your age so I spit out the following in your general direction"); System.out.println(e); // use the exception class's toString method } try { double income = RedmondMsgIn.readValidDoubleRobust("How much did you make this week",0.0,1200.00); System.out.println("Your weekly earnings are: " + income); } // catch user cancelled catch (UserCancelException e) { // not much to do in this case System.out.println("You refused to give your income so I spit out the following in your general direction"); System.out.println(e); // use the exception class's toString method } int finalAnswer = RedmondMsgIn.readValidInt("Please enter your age",0,120); System.out.println("Your age is: " + finalAnswer); int temperature = RedmondMsgIn.readValidInt("Please enter the current temperature in degrees fahrenheit",-5,110); System.out.println("The current temperature is: " + temperature); System.exit(0); } }