/* * RedmondMsgInBasic.java * * Created on January 6, 2005, 9:42 AM * Copy of version 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 RedmondMsgInBasic { /** Creates a new instance of RedmondMsgInBasic */ public RedmondMsgInBasic() { } /** * 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); // copy to output pane for checking history System.out.println(message); int answer; // convert output to integer // this will bomb if user enters a char or a double - watch out answer = Integer.parseInt(answerString); // echo users answer to output pane for checking history System.out.println(answer); // 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 + ")"); // copy to output pane for checking history System.out.println(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); // echo users answer to output pane for checking history System.out.println(answer); } // 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 * 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 = RedmondMsgInBasic.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 = RedmondMsgInBasic.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); // copy to output pane for checking history System.out.println(message); double answer; // convert output to double // this will bomb if user enters a char - watch out answer = Double.parseDouble(answerString); // echo users answer to output pane for checking history System.out.println(answer); // 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 + ")"); // copy to output pane for checking history System.out.println(message+ " (between " + min + " and " + max + ")"); // convert output to double // this will bomb if user enters a char - watch out answer = Double.parseDouble(answerString); // echo users answer to output pane for checking history System.out.println(answer); } // if we get here - we have a valid input - return it return answer; } /** * 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); // copy to output pane for checking history System.out.println(message); char answer; // take only the first char // this will not work well if user enters leading space(s) answer = answerString.charAt(0); // echo users answer to output pane for checking history System.out.println(answer); // 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) "); // copy to output pane for checking history System.out.println(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); // echo users answer to output pane for checking history System.out.println(answer); } // 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); // copy to output pane for checking history System.out.println(message); // echo users answer to output pane for checking history System.out.println(answerString); if (answerString == null) { return ""; } else { // trim extra white space answerString = answerString.trim(); return answerString; } } /** * @param args the command line arguments * little test driver */ public static void main (String args[]) { char yn = RedmondMsgInBasic.readValidYN("Do you want to play hearts?"); System.out.println("echo: " + yn); //String answ = RedmondMsgInBasic.readString("What is your pin?"); //System.out.println("PIN: " + answ); String answ = RedmondMsgInBasic.readString("What is your pin?"); System.out.println("PIN: " + answ); int age = RedmondMsgInBasic.readValidInt("Please enter your age",0,120); System.out.println("Your age is: " + age); double income = RedmondMsgInBasic.readValidDouble("How much did you make this week",0.0,1200.00); System.out.println("Your weekly earnings are: " + income); int finalAnswer = RedmondMsgInBasic.readValidInt("Please enter your age",0,120); System.out.println("Your age is: " + finalAnswer); int temperature = RedmondMsgInBasic.readValidInt("Please enter the current temperature in degrees fahrenheit",-5,110); System.out.println("The current temperature is: " + temperature); System.exit(0); } }