public class ValidatedInputReader
extends java.lang.Object
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.)
Constructor and Description |
---|
ValidatedInputReader() |
Modifier and Type | Method and Description |
---|---|
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. |
public static byte getByte(java.lang.String initialPrompt, byte suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the usersuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static byte getByte(java.lang.String initialPrompt, byte startOfRange, byte endOfRange, byte suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the userstartOfRange
- the smallest value in the range of valid valuesendOfRange
- the largest value in the range of valid valuessuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static short getShort(java.lang.String initialPrompt, short suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the usersuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static short getShort(java.lang.String initialPrompt, short startOfRange, short endOfRange, short suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the userstartOfRange
- the smallest value in the range of valid valuesendOfRange
- the largest value in the range of valid valuessuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static int getInteger(java.lang.String initialPrompt, int suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the usersuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static int getInteger(java.lang.String initialPrompt, int startOfRange, int endOfRange, int suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the userstartOfRange
- the smallest value in the range of valid valuesendOfRange
- the largest value in the range of valid valuessuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static long getLong(java.lang.String initialPrompt, long suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the usersuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static long getLong(java.lang.String initialPrompt, long startOfRange, long endOfRange, long suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the userstartOfRange
- the smallest value in the range of valid valuesendOfRange
- the largest value in the range of valid valuessuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static float getFloat(java.lang.String initialPrompt, float suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the usersuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static float getFloat(java.lang.String initialPrompt, float startOfRange, float endOfRange, float suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the userstartOfRange
- the smallest value in the range of valid valuesendOfRange
- the largest value in the range of valid valuessuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static double getDouble(java.lang.String initialPrompt, double suggestedValue, java.lang.String clarificationPrompt)
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.initialPrompt
- the initial prompt to the usersuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static double getDouble(java.lang.String initialPrompt, double startOfRange, double endOfRange, double suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the userstartOfRange
- the smallest value in the range of valid valuesendOfRange
- the largest value in the range of valid valuessuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static char getChar(java.lang.String initialPrompt, char suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the usersuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid datapublic static java.lang.String getString(java.lang.String initialPrompt, java.lang.String suggestedValue)
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.initialPrompt
- the initial prompt to the usersuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects Cancelpublic static boolean getBoolean(java.lang.String initialPrompt, boolean suggestedValue, java.lang.String clarificationPrompt)
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
.initialPrompt
- the initial prompt to the usersuggestedValue
- a suggested, valid value that is displayed
to the user, and is also used as the default
value if the user selects CancelclarificationPrompt
- a follow-up prompt for input after
the user has input invalid dataprotected static java.lang.String getResponse(java.lang.String prompt, java.lang.String suggestedValue)