X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=svjatoslav_commons.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fcommandline%2FCLIHelper.java;h=be7e52504d371b0cc5b1eec194cb95599e3c7d5f;hp=5a774e2bfe6bff7a2f249abcbb7b8552b347c984;hb=9bf004ce4e9b5edff36c65fcc8cc0f303390d7fc;hpb=afaa928dd10304ee3e8e6bad3a377ced6a7b2f42 diff --git a/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java b/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java index 5a774e2..be7e525 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java @@ -18,94 +18,92 @@ import java.io.InputStreamReader; */ public class CLIHelper { - /** - * Ask boolean value from user on command-line. - * - * @param prompt - * to show to the user - * @return true is user answered affirmative. - */ - public static boolean askBoolean(final String prompt) { - - final BufferedReader br = new BufferedReader(new InputStreamReader( - System.in)); - - while (true) - try { - System.out.print(prompt); - - final String userInput = br.readLine().toLowerCase(); - - if ("y".equals(userInput) || "yes".equals(userInput) - || "true".equals(userInput)) - return true; - - if ("n".equals(userInput) || "no".equals(userInput) - || "false".equals(userInput)) - return false; - - System.out - .println("Invalid input. You shall enter y/yes/true or n/no/false."); - } catch (final IOException ioe) { - ioe.printStackTrace(); - } - } - - /** - * Ask numerical long value from user on command-line. - * - * @param prompt - * to show to the user - * @return value given by user - */ - public static long askLong(final String prompt) { - - final BufferedReader br = new BufferedReader(new InputStreamReader( - System.in)); - - while (true) { - System.out.print(prompt); - - try { - final String userInput = br.readLine(); - - try { - final long result = Long.parseLong(userInput); - return result; - } catch (final NumberFormatException e) { - System.out.println("\nError: You shall enter an integer."); - } - } catch (final IOException ioe) { - ioe.printStackTrace(); - } - - } - } - - /** - * Ask string value from user on command-line. - * - * @param prompt - * to show to the user - * @return value given by the user - */ - public static String askString(final String prompt) { - - final BufferedReader br = new BufferedReader(new InputStreamReader( - System.in)); - - while (true) { - System.out.print(prompt); - - try { - final String userInput = br.readLine(); - - return userInput; - } catch (final IOException ioe) { - ioe.printStackTrace(); - } - - } - } + /** + * Ask boolean value from user on command-line. + * + * @param prompt to show to the user + * @return true is user answered affirmative. + */ + public static boolean askBoolean(final String prompt) { + + final BufferedReader br = new BufferedReader(new InputStreamReader( + System.in)); + + while (true) + try { + System.out.print(prompt); + + String line = br.readLine(); + if (line != null) { + final String userInput = line.toLowerCase(); + + if ("y".equals(userInput) || "yes".equals(userInput) + || "true".equals(userInput)) + return true; + + if ("n".equals(userInput) || "no".equals(userInput) + || "false".equals(userInput)) + return false; + } + System.out + .println("Invalid input. You shall enter y/yes/true or n/no/false."); + } catch (final IOException ioe) { + ioe.printStackTrace(); + } + } + + /** + * Ask numerical long value from user on command-line. + * + * @param prompt to show to the user + * @return value given by user + */ + public static long askLong(final String prompt) { + + final BufferedReader br = new BufferedReader(new InputStreamReader( + System.in)); + + while (true) { + System.out.print(prompt); + + try { + final String userInput = br.readLine(); + + try { + return Long.parseLong(userInput); + } catch (final NumberFormatException e) { + System.out.println("\nError: You shall enter an integer."); + } + } catch (final IOException ioe) { + ioe.printStackTrace(); + } + + } + } + + /** + * Ask string value from user on command-line. + * + * @param prompt to show to the user + * @return value given by the user + */ + public static String askString(final String prompt) { + + final BufferedReader br = new BufferedReader(new InputStreamReader( + System.in)); + + while (true) { + System.out.print(prompt); + + try { + final String userInput = br.readLine(); + + return userInput; + } catch (final IOException ioe) { + ioe.printStackTrace(); + } + + } + } }