diff --git a/02/src/de/dis2017/FormUtil.java b/02/src/de/dis2017/FormUtil.java index d269edf..08868c6 100644 --- a/02/src/de/dis2017/FormUtil.java +++ b/02/src/de/dis2017/FormUtil.java @@ -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(); } @@ -39,19 +51,31 @@ public class FormUtil { password = String.valueOf(System.console().readPassword()); 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);