|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--lava.io.ConsoleDialog
Console-based input manager.
Details: ConsoleDialog
offers a set of methods which are useful for interacting with users in console mode applications. Prompts are typically sent to the user through System.out
, and responses are received from the user through System.in
, but both streams can be customized.
The primary benefit of using ConsoleDialog
is that it makes obtaining certain kinds of input easy. For example, you can call a method that requests an integer from the user and know that the return value will be a valid integer, since it will not return until the user complies. Deep in the call, that method automatically handles parsing, re-prompts after invalid responses, etc., so you don't have to.
query* methods
The query
* methods prompt the user for information and then wait for a response. If the user's response is valid, the method returns with the data. If the user's response is not valid, then the action taken depends on the value of the error_response
parameter passed in.
error_response != null
error_response
string is output to the console and the user is prompted for the information again. (Note that a newline character is not automatically appended to the error message.) The method will never return until the user has entered a valid response.
error_response == null
error_response
was omitted from the parameter call
Note that in case (2), it may be difficult to discern valid responses from failure values if the failure values lie within the range of valid responses. (See the documentation for each method for more information on which methods return which failure values.) In cases (1) and (3), the methods do not return until the user has entered a valid response.
Spaces before and after user responses are allowed, but always ignored. If the first part of a response is valid but the line contains additional input, the entire response is rejected. If an EOF signal is detected while reading the user's response, an EOFException
is thrown.
Field Summary | |
java.lang.String |
default_error_response
Default response to be used when the error response parameter is omitted from a query method call. |
Constructor Summary | |
ConsoleDialog()
Constructs an instance that sends prompts to System.out and receives responses from System.in . |
|
ConsoleDialog(java.io.Reader reader)
Constructs an instance that sends prompts to System.out and receives responses from the specified Reader . |
|
ConsoleDialog(java.io.Reader reader,
java.io.Writer writer)
Constructs an instance that sends prompts to the specified output stream and reads responses from the specified input stream. |
Method Summary | |
char |
queryChar(java.lang.String prompt)
Queries the user for a single character, using the default error response. |
char |
queryChar(java.lang.String prompt,
java.lang.String error_response)
Queries the user for a single character. |
float |
queryFloat(java.lang.String prompt)
Queries the user for a float . |
float |
queryFloat(java.lang.String prompt,
java.lang.String error_response)
Queries the user for a float . |
java.io.File |
queryInputFile(java.lang.String prompt,
java.lang.String directory)
See queryInputFile(String,String,String) . |
java.io.File |
queryInputFile(java.lang.String prompt,
java.lang.String directory,
java.lang.String error_response)
Queries the user for an output filename. |
int |
queryInt(java.lang.String prompt)
Queries the user for an int . |
int |
queryInt(java.lang.String prompt,
int radix)
Queries the user for an int . |
int |
queryInt(java.lang.String prompt,
int radix,
java.lang.String error_response)
Queries the user for an int . |
java.io.File |
queryOutputFile(java.lang.String prompt,
java.lang.String directory)
See queryOutputFile(String,String,String) . |
java.io.File |
queryOutputFile(java.lang.String prompt,
java.lang.String directory,
java.lang.String error_response)
Queries the user for an output filename. |
java.lang.String |
queryWord(java.lang.String prompt)
Queries the user for a single word or C-style quoted string. |
java.lang.String |
queryWord(java.lang.String prompt,
java.lang.String error_response)
Queries the user for a single word or C-style quoted string. |
java.lang.String[] |
queryWords(java.lang.String prompt)
Queries the user for a list of words. |
java.lang.String[] |
queryWords(java.lang.String prompt,
java.lang.String error_response)
Queries the user for a list of words. |
java.lang.String |
readLine()
Reads a line of input. |
void |
send(java.lang.String s)
Writes a string to the output stream and flushes. |
void |
sendLine(java.lang.String s)
Writes a line to the output stream and flushes. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public java.lang.String default_error_response
query
method call.Constructor Detail |
public ConsoleDialog(java.io.Reader reader, java.io.Writer writer)
reader
- the input streamwriter
- the output streampublic ConsoleDialog(java.io.Reader reader)
System.out
and receives responses from the specified Reader
.public ConsoleDialog()
System.out
and receives responses from System.in
.Method Detail |
public void send(java.lang.String s) throws java.io.IOException
public void sendLine(java.lang.String s) throws java.io.IOException
public java.lang.String readLine() throws java.io.IOException
null
if EOFpublic float queryFloat(java.lang.String prompt, java.lang.String error_response) throws java.io.IOException
float
. A valid response is any string of characters which can reasonably be interpreted as a float
. For more information about this method, see the general description of query
* methods above. Failure value: Float.MIN_VALUE
.prompt
- the prompterror_response
- the error response (default: default_error_response
)public float queryFloat(java.lang.String prompt) throws java.io.IOException
float
. Supplies default parameters for queryFloat(String,String)
.public int queryInt(java.lang.String prompt, int radix, java.lang.String error_response) throws java.io.IOException
int
. A valid response is any string of characters which can be interpreted as a signed 32-bit integer using the given radix. Radix-specific prefixes, such as "0x", are not allowed. For more information about this method, see the general description of query
* methods above. Failure value: Integer.MIN_VALUE
.prompt
- the promptradix
- the radix (default: 10)error_response
- the error response (default: default_error_response
)public int queryInt(java.lang.String prompt, int radix) throws java.io.IOException
int
. Supplies default parameters for queryInt(String,int,String)
.public int queryInt(java.lang.String prompt) throws java.io.IOException
int
. Supplies default parameters for queryInt(String,int,String)
.public java.lang.String[] queryWords(java.lang.String prompt, java.lang.String error_response) throws java.io.IOException
query
* methods above. If the line entered is blank, a 0-length array is returned. (Note that a blank line is not considered a failure.)
Failure value: null
.
prompt
- the prompterror_response
- the error response (Default value: default_error_response
)String
s containing the words entered by the userpublic java.lang.String[] queryWords(java.lang.String prompt) throws java.io.IOException
queryWords(String,String)
.public java.lang.String queryWord(java.lang.String prompt, java.lang.String error_response) throws java.io.IOException
query
* methods above.
Failure value: null
.
prompt
- the prompterror_response
- the error responsepublic java.lang.String queryWord(java.lang.String prompt) throws java.io.IOException
queryWord(String,String)
.public char queryChar(java.lang.String prompt, java.lang.String error_response) throws java.io.IOException
query
* methods above.
Failure value: 0
.
prompt
- the prompterror_response
- the error responsepublic char queryChar(java.lang.String prompt) throws java.io.IOException
query
* methods above.prompt
- the promptpublic java.io.File queryInputFile(java.lang.String prompt, java.lang.String directory, java.lang.String error_response) throws java.io.IOException
directory
parameter will be used to resolve the filename.
For more information about this method, see the general description of query
* methods above.
Failure value: null
.
Note: The following boolean condition is tested to make sure the filename entered is a valid file:
file . exists () && file . isFile () && file . canRead ()
where file
is a java.io.File
object.prompt
- the promptdirectory
- the starting directory for relative filenameserror_response
- the error responseFile
object refering to the filepublic java.io.File queryInputFile(java.lang.String prompt, java.lang.String directory) throws java.io.IOException
queryInputFile(String,String,String)
.public java.io.File queryOutputFile(java.lang.String prompt, java.lang.String directory, java.lang.String error_response) throws java.io.IOException
directory
parameter will be used to resolve the filename.
For more information about this method, see the general description of query
* methods above.
Failure value: null
.
Note: The following boolean condition is tested to make sure the filename entered is a valid file:
file . canWrite ()
where file
is a java.io.File
object.prompt
- the promptdirectory
- the starting directory for relative filenameserror_response
- the error responseFile
object refering to the selected output filepublic java.io.File queryOutputFile(java.lang.String prompt, java.lang.String directory) throws java.io.IOException
queryOutputFile(String,String,String)
.
|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |