Added method variants with default values
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
@ -7,20 +7,32 @@ import java.io.InputStreamReader;
|
||||
/**
|
||||
* Small helper class for forms.
|
||||
*/
|
||||
public class FormUtil {
|
||||
/**
|
||||
class FormUtil {
|
||||
/**
|
||||
* Reads a string from the console.
|
||||
*
|
||||
* @param label Label that is shown before the input
|
||||
* @return read string
|
||||
*/
|
||||
static String readString(String label) {
|
||||
return readString(label, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a string from the console.
|
||||
*
|
||||
* @param label Label that is shown before the input
|
||||
* @param defaultValue the default value in case an empty input is provided
|
||||
* @return read string
|
||||
*/
|
||||
static String readString(String label) {
|
||||
static String readString(String label, String defaultValue) {
|
||||
String ret = null;
|
||||
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
try {
|
||||
System.out.print(label+": ");
|
||||
ret = stdin.readLine();
|
||||
ret = ret.isEmpty() ? defaultValue : ret;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -40,18 +52,30 @@ public class FormUtil {
|
||||
return password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an integer from the console.
|
||||
*
|
||||
* @param label Label that is shown before the input
|
||||
* @return read integer
|
||||
*/
|
||||
static int readInt(String label) {
|
||||
return readInt(label, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an integer from the console.
|
||||
*
|
||||
* @param label Label that is shown before the input
|
||||
* @param defaultValue the default value
|
||||
* @return read integer
|
||||
*/
|
||||
public static int readInt(String label) {
|
||||
static int readInt(String label, int defaultValue) {
|
||||
int ret = 0;
|
||||
boolean finished = false;
|
||||
String defaultValueStr = defaultValue == -1 ? "" : String.valueOf(defaultValue);
|
||||
|
||||
while(!finished) {
|
||||
String line = readString(label);
|
||||
String line = readString(label, defaultValueStr);
|
||||
|
||||
try {
|
||||
ret = Integer.parseInt(line);
|
||||
|
||||
Reference in New Issue
Block a user