edu.kzoo.util
Class ValidatedInputReader

java.lang.Object
  |
  +--edu.kzoo.util.ValidatedInputReader

public class ValidatedInputReader
extends java.lang.Object

Kalamazoo College Utility Classes:
This class provides numerous static methods that will put up a dialog box prompting the user for input and validate the responses. It handles all of the primitive Java types including: byte, short, int, long, float, double, char, String, and boolean.

Each method displays an initial prompt with a suggested value, waits for the user's response, and validates it. If the user's response is invalid, the method prompts the user again using a clarification prompt until the user enters a valid response or selects Cancel. If the user selects Cancel, the input method returns the suggested value as a default. Thus, each input method requires an initial prompt, a suggested value, and a clarification prompt. Input methods for numeric values may also provide a range of valid values. (This input style was inspired by the Java Power Tools package developed by Richard Rasala, Jeff Raab, and Viera Proulx at Northeastern University.)

Version:
September 3, 2004
Author:
Joel Booth, Alyce Brady

Constructor Summary
ValidatedInputReader()
           
 
Method Summary
static boolean getBoolean(java.lang.String initialPrompt, boolean suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it.
static byte getByte(java.lang.String initialPrompt, byte startOfRange, byte endOfRange, byte suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid byte in the range provided).
static byte getByte(java.lang.String initialPrompt, byte suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid byte).
static char getChar(java.lang.String initialPrompt, char suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid char).
static double getDouble(java.lang.String initialPrompt, double startOfRange, double endOfRange, double suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid double in the range provided).
static double getDouble(java.lang.String initialPrompt, double suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid double).
static float getFloat(java.lang.String initialPrompt, float startOfRange, float endOfRange, float suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid float in the range provided).
static float getFloat(java.lang.String initialPrompt, float suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid float).
static int getInteger(java.lang.String initialPrompt, int startOfRange, int endOfRange, int suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid integer in the range provided).
static int getInteger(java.lang.String initialPrompt, int suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid integer).
static long getLong(java.lang.String initialPrompt, long startOfRange, long endOfRange, long suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid long in the range provided).
static long getLong(java.lang.String initialPrompt, long suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid long).
protected static java.lang.String getResponse(java.lang.String prompt, java.lang.String suggestedValue)
          Displays the appropriate JOptionPane and gets the result.
static short getShort(java.lang.String initialPrompt, short startOfRange, short endOfRange, short suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid short in the range provided).
static short getShort(java.lang.String initialPrompt, short suggestedValue, java.lang.String clarificationPrompt)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid short).
static java.lang.String getString(java.lang.String initialPrompt, java.lang.String suggestedValue)
          Puts up a dialog box displaying the initialPrompt, waits for the user's response.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ValidatedInputReader

public ValidatedInputReader()
Method Detail

getByte

public static byte getByte(java.lang.String initialPrompt,
                           byte suggestedValue,
                           java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid byte). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getByte returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getByte

public static byte getByte(java.lang.String initialPrompt,
                           byte startOfRange,
                           byte endOfRange,
                           byte suggestedValue,
                           java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid byte in the range provided). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getByte returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
startOfRange - the smallest value in the range of valid values
endOfRange - the largest value in the range of valid values
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getShort

public static short getShort(java.lang.String initialPrompt,
                             short suggestedValue,
                             java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid short). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getShort returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getShort

public static short getShort(java.lang.String initialPrompt,
                             short startOfRange,
                             short endOfRange,
                             short suggestedValue,
                             java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid short in the range provided). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getShort returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
startOfRange - the smallest value in the range of valid values
endOfRange - the largest value in the range of valid values
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getInteger

public static int getInteger(java.lang.String initialPrompt,
                             int suggestedValue,
                             java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid integer). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getInteger returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getInteger

public static int getInteger(java.lang.String initialPrompt,
                             int startOfRange,
                             int endOfRange,
                             int suggestedValue,
                             java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid integer in the range provided). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getInteger returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
startOfRange - the smallest value in the range of valid values
endOfRange - the largest value in the range of valid values
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getLong

public static long getLong(java.lang.String initialPrompt,
                           long suggestedValue,
                           java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid long). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getLong returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getLong

public static long getLong(java.lang.String initialPrompt,
                           long startOfRange,
                           long endOfRange,
                           long suggestedValue,
                           java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid long in the range provided). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getLong returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
startOfRange - the smallest value in the range of valid values
endOfRange - the largest value in the range of valid values
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getFloat

public static float getFloat(java.lang.String initialPrompt,
                             float suggestedValue,
                             java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid float). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getFloat returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getFloat

public static float getFloat(java.lang.String initialPrompt,
                             float startOfRange,
                             float endOfRange,
                             float suggestedValue,
                             java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid float in the range provided). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getFloat returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
startOfRange - the smallest value in the range of valid values
endOfRange - the largest value in the range of valid values
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getDouble

public static double getDouble(java.lang.String initialPrompt,
                               double suggestedValue,
                               java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid double). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getDouble returns the suggestedValue. Any valid double value will be accepted. If the number is out of the range of a double the value will show up as the double representation of either positive or negative inifinity.

Parameters:
initialPrompt - the initial prompt to the user
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getDouble

public static double getDouble(java.lang.String initialPrompt,
                               double startOfRange,
                               double endOfRange,
                               double suggestedValue,
                               java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid double in the range provided). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getDouble returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
startOfRange - the smallest value in the range of valid values
endOfRange - the largest value in the range of valid values
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getChar

public static char getChar(java.lang.String initialPrompt,
                           char suggestedValue,
                           java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it (the response must be a valid char). Special characters are accepted. Any empty response will be taken as the null character. If the user's response is not a valid character, getChar prompts again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getChar returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getString

public static java.lang.String getString(java.lang.String initialPrompt,
                                         java.lang.String suggestedValue)
Puts up a dialog box displaying the initialPrompt, waits for the user's response. Because it is prompting for a String, any response will be accepted, including an empty string. If the user selects Cancel the suggestedValue. will be returned.

Parameters:
initialPrompt - the initial prompt to the user
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel

getBoolean

public static boolean getBoolean(java.lang.String initialPrompt,
                                 boolean suggestedValue,
                                 java.lang.String clarificationPrompt)
Puts up a dialog box displaying the initialPrompt, waits for the user's response, and validates it. The response must be a valid boolean in the form of "true" or "false" (case-insensitive). If the user's response is invalid, it prompts the user again using the clarificationPrompt until the user enters a valid response or selects Cancel. If the user selects Cancel, getBoolean returns the suggestedValue.

Parameters:
initialPrompt - the initial prompt to the user
suggestedValue - a suggested, valid value that is displayed to the user, and is also used as the default value if the user selects Cancel
clarificationPrompt - a follow-up prompt for input after the user has input invalid data

getResponse

protected static java.lang.String getResponse(java.lang.String prompt,
                                              java.lang.String suggestedValue)
Displays the appropriate JOptionPane and gets the result.