Package org.jcsp.demos.util
Class Ask
- java.lang.Object
-
- org.jcsp.demos.util.Ask
-
public class Ask extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Ask.Prompt
-
Field Summary
Fields Modifier and Type Field Description private static java.awt.Container
buttonContainer
private static java.lang.String
descr
private static java.io.BufferedReader
in
private static java.io.InputStreamReader
isr
This Ask class contains a set of static methods for safe interactive input of individual primitive types.private static boolean
oneTokenReply
If oneTokenReply, extra tokens in replies cause the input to be rejected.private static java.util.Hashtable
prompts
private static java.lang.String
title
-
Constructor Summary
Constructors Modifier Constructor Description private
Ask()
Don't allow any instances!
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addPrompt(java.lang.String prompt)
static void
addPrompt(java.lang.String prompt, int min, int max, int def)
static void
app(java.lang.String prompt, java.lang.String description)
static void
blank()
static boolean
Boolean(java.lang.String prompt)
Ask.Boolean issues the prompt and returns a boolean depending on the first non-white-space character typed.static byte
Byte(java.lang.String prompt, byte min, byte max)
Ask.Byte issues the prompt and returns a byte between min and max inclusive.static char
Char(java.lang.String prompt, char[] valid)
Ask.Char issues the prompt and returns a char depending on the first non-white-space character typed.static double
Double(java.lang.String prompt, double min, double max)
Ask.Double issues the prompt and returns a double between min and max inclusive.static float
Float(java.lang.String prompt, float min, float max)
Ask.Float issues the prompt and returns a float between min and max inclusive.static boolean
getOneTokenReply()
Ask.getOneTokenReply returns whether multiple response tokens will be rejected.static int
Int(java.lang.String prompt, int min, int max)
Ask.Int issues the prompt and returns an int between min and max inclusive.static long
Long(java.lang.String prompt, long min, long max)
Ask.Long issues the prompt and returns an long between min and max inclusive.static int
readInt(java.lang.String prompt)
static java.lang.String
readStr(java.lang.String prompt)
static void
setOneTokenReply(boolean b)
Ask.setOneTokenReply (true) specifies that more than one token in the user reply will be rejected, even if the first token is valid.static short
Short(java.lang.String prompt, short min, short max)
Ask.Short issues the prompt and returns a short between min and max inclusive.static void
show()
static java.lang.String
string(java.lang.String prompt)
Ask.string issues the prompt and returns the String that is typed.static void
Void(java.lang.String prompt)
Ask.Void issues the prompt and returns whenis pressed.
-
-
-
Field Detail
-
buttonContainer
private static java.awt.Container buttonContainer
-
prompts
private static java.util.Hashtable prompts
-
title
private static java.lang.String title
-
descr
private static java.lang.String descr
-
isr
private static java.io.InputStreamReader isr
This Ask class contains a set of static methods for safe interactive input of individual primitive types. They will not return until an acceptable answer has been entered. The implementation techniques draw on those in the `Java Gently' textbook by Judy Bishop.
-
in
private static java.io.BufferedReader in
-
oneTokenReply
private static boolean oneTokenReply
If oneTokenReply, extra tokens in replies cause the input to be rejected.
-
-
Method Detail
-
app
public static void app(java.lang.String prompt, java.lang.String description)
-
addPrompt
public static void addPrompt(java.lang.String prompt, int min, int max, int def)
-
addPrompt
public static void addPrompt(java.lang.String prompt)
-
readInt
public static int readInt(java.lang.String prompt)
-
readStr
public static java.lang.String readStr(java.lang.String prompt)
-
show
public static void show()
-
blank
public static void blank()
-
Int
public static int Int(java.lang.String prompt, int min, int max)
Ask.Int issues the prompt and returns an int between min and max inclusive. Keyboard errors in typing the reply are all trapped. The method will not return until a valid int has been entered in the indicated range.- Parameters:
prompt
- the string used to prompt for input.min
- user input must have a value >= min.max
- user input must have a value <= max.- Returns:
- user input complying with the rules.
-
Long
public static long Long(java.lang.String prompt, long min, long max)
Ask.Long issues the prompt and returns an long between min and max inclusive. Keyboard errors in typing the reply are all trapped. The method will not return until a valid long has been entered in the indicated range.- Parameters:
prompt
- the string used to prompt for input.min
- user input must have a value >= min.max
- user input must have a value <= max.- Returns:
- user input complying with the rules.
-
Byte
public static byte Byte(java.lang.String prompt, byte min, byte max)
Ask.Byte issues the prompt and returns a byte between min and max inclusive. Keyboard errors in typing the reply are all trapped. The method will not return until a valid byte has been entered in the indicated range.- Parameters:
prompt
- the string used to prompt for input.min
- user input must have a value >= min.max
- user input must have a value <= max.- Returns:
- user input complying with the rules.
-
Short
public static short Short(java.lang.String prompt, short min, short max)
Ask.Short issues the prompt and returns a short between min and max inclusive. Keyboard errors in typing the reply are all trapped. The method will not return until a valid short has been entered in the indicated range.- Parameters:
prompt
- the string used to prompt for input.min
- user input must have a value >= min.max
- user input must have a value <= max.- Returns:
- user input complying with the rules.
-
Float
public static float Float(java.lang.String prompt, float min, float max)
Ask.Float issues the prompt and returns a float between min and max inclusive. Keyboard errors in typing the reply are all trapped. The method will not return until a valid float has been entered in the indicated range.- Parameters:
prompt
- the string used to prompt for input.min
- user input must have a value >= min.max
- user input must have a value <= max.- Returns:
- user input complying with the rules.
-
Double
public static double Double(java.lang.String prompt, double min, double max)
Ask.Double issues the prompt and returns a double between min and max inclusive. Keyboard errors in typing the reply are all trapped. The method will not return until a valid double has been entered in the indicated range.- Parameters:
prompt
- the string used to prompt for input.min
- user input must have a value >= min.max
- user input must have a value <= max.- Returns:
- user input complying with the rules.
-
Boolean
public static boolean Boolean(java.lang.String prompt)
Ask.Boolean issues the prompt and returns a boolean depending on the first non-white-space character typed. 'y' or 'Y' cause true to be returned, while 'n' or 'N' cause false to be returned. Anything else is rejected.- Parameters:
prompt
- the string used to prompt for input.- Returns:
- whether the user typed yes or no (according to the above rules).
-
Char
public static char Char(java.lang.String prompt, char[] valid)
Ask.Char issues the prompt and returns a char depending on the first non-white-space character typed. This character must be one in the valid character array. Anything else is rejected.- Parameters:
prompt
- the string used to prompt for input.valid
- the array of characters defining valid user responses.- Returns:
- user input complying with the rules.
-
Void
public static void Void(java.lang.String prompt)
Ask.Void issues the prompt and returns whenis pressed. - Parameters:
prompt
- the string used to prompt for a.
-
string
public static java.lang.String string(java.lang.String prompt)
Ask.string issues the prompt and returns the String that is typed.- Parameters:
prompt
- the string used to prompt for input.- Returns:
- whatever the user inputs.
-
setOneTokenReply
public static void setOneTokenReply(boolean b)
Ask.setOneTokenReply (true) specifies that more than one token in the user reply will be rejected, even if the first token is valid. This is the default condition for this class.Ask.setOneTokenReply (false) specifies that multiple tokens are allowed, but that only the first will be processed.
The false allows user input to be documented with comments (in the second and following tokens) explaining the meaning of the first token, which contains the actual data. This is useful when preparing user input as a file, from which the standard input stream will later be redirected, so that each line of input can be documented.
- Parameters:
b
- if true, the response must consist of a single token - otherwise, multiple tokens are allowed but only the first will be processed.
-
getOneTokenReply
public static boolean getOneTokenReply()
Ask.getOneTokenReply returns whether multiple response tokens will be rejected.- Returns:
- whether multiple response tokens will be rejected.
-
-