Getting Input - Sam36502/SimpleTUI GitHub Wiki
Setup
All input requires the input scanner to be open. For more info see Input Setup.
Strings
Getting a string is the simplest input as it requires no inherant validation.
To prompt the user and get a string, one must simple use the Input.getString()
method. This will returns whatever the user entered.
Integers
If you just want any integer within the signed integer range (-2,147,483,648 to 2,147,483,647)
the Input.getInt()
function will do that. The user will be prompted and any integer they enter
will be returned by the function. If the user enters a number outside of the signed integer range,
they will be told that the number is too large or too small, and they will be prompted again. The
method doesn't return until a valid integer has been entered.
If you would like to limit the range of possible values further, you can provide minimum and
maximum values. E.g.: Input.getInt(1, 5)
. Then if the user enters anything greater than the
maximum value or less than the minimum value, they are told their input is invalid and prompted
again.
Example:
Input.getInt(1, 5);
--> 10
Warning: Only whole numbers between 1 and 5 are allowed.
-->
You might want to limit the possible inputs even further to only a list of set values. This
can be done by specifying at least three possible options like so: Input.getInt(2, 4, 6, 8)
.
Then, the user may only enter integers in that list. If their input value is not in the given
options, they are informed their input is invalid and prompted again.
Example:
Input.getInt(2, 4, 6, 8);
--> 3
Warning: Only the numbers 2, 4, 6 and 8 are allowed.
-->
Doubles
If you require more precision in your input, the Input.getDouble()
method works nearly
identically to getInt()
, but it returns a double instead.
Examples:
Input.getDouble();
--> 3.14159
// Returns 3.14159
Input.getDouble(0.001, 0.999);
--> 1
Warning: Only numbers between 0.001 and 0.999 are allowed.
--> 0.5
// Returns 0.5
Input.getDouble(1.41, 3.14, 4.20);
--> 0.5
Warning: Only the numbers 1.41, 3.14 and 4.20 are allowed.
--> 4.20
// Returns 4.20
Booleans
You might want to ask the user a simple yes/no question and get their
answer easily. This is done via the Input.getBool()
method. The user
will be prompted with the usual prompt as well as "(y/n)". If the user
enters "yes" or "y", the function returns true. Otherwise, if the input
is "no" or "n", the function returns false.
Example:
Input.getBool();
(y/n) --> blah
Warning: Only yes or no answers are allowed.
(y/n) --> yes
// Returns true
(y/n) --> n
// Returns false
Files
Although not much work has been done yet on file input, the Input class
offers a way to get File objects from the user. If the Input.getFile()
method is used, the user is prompted to enter a filename. If the file
exists and can be found, the method returns a File
object that references
the given file. Otherwise the user is informed that their file couldn't be
found and is prompted again.
Example:
Input.getFile();
--> nonexistant.txt
Warning: That file doesn't exist.
--> this/file/exists.txt
// Returns a File object for this file