Improve user interaction.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 19 Mar 2025 22:22:41 +0000 (00:22 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 19 Mar 2025 22:22:41 +0000 (00:22 +0200)
Better ways to ask long, float and string inputs from user.

src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java

index 184585b..08354b7 100755 (executable)
@@ -7,6 +7,11 @@ package eu.svjatoslav.commons.cli_helper;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.util.Scanner;
+
+import static java.lang.Float.parseFloat;
+import static java.lang.Long.parseLong;
+import static java.lang.String.valueOf;
 
 /**
  * Command-line interface helper.
@@ -48,55 +53,70 @@ public class CLIHelper {
     }
 
     /**
-     * Ask numerical long value from user on command-line.
+     * Ask numerical double value from user on command-line.
      *
      * @param prompt to show to the user
+     * @param defaultValue Default value.
+     * @param min Minimum value (inclusive).
+     * @param max Maximum value (inclusive).
      * @return value given by user
      */
-    public static long askLong(final String prompt) {
-
-        final BufferedReader br = new BufferedReader(new InputStreamReader(
-                System.in));
-
+    private float askFloat(String prompt, float defaultValue, float min, float max) {
         while (true) {
-            System.out.print(prompt);
-
+            String input = askString(prompt, valueOf(defaultValue));
             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();
+                float value = parseFloat(input);
+                if (value >= min && value <= max) return value;
+                System.out.println("Value must be between " + min + " and " + max);
+            } catch (NumberFormatException e) {
+                System.out.println("Invalid number format. Try again.");
             }
-
         }
     }
 
     /**
-     * Ask string value from user on command-line.
+     * Ask numerical double value from user on command-line.
      *
      * @param prompt to show to the user
-     * @return value given by the user
+     * @param defaultValue Default value.
+     * @param min Minimum value (inclusive).
+     * @param max Maximum value (inclusive).
+     * @return value given by user
      */
-    public static String askString(final String prompt) {
-
-        final BufferedReader br = new BufferedReader(new InputStreamReader(
-                System.in));
-
+    private float askLong(String prompt, float defaultValue, long min, long max) {
         while (true) {
-            System.out.print(prompt);
-
+            String input = askString(prompt, valueOf(defaultValue));
             try {
-                return br.readLine();
-            } catch (final IOException ioe) {
-                ioe.printStackTrace();
+                float value = parseLong(input);
+                if (value >= min && value <= max) return value;
+                System.out.println("Value must be between " + min + " and " + max);
+            } catch (NumberFormatException e) {
+                System.out.println("Invalid number format. Try again.");
             }
-
         }
     }
 
+    /**
+     * Ask string value from user on command-line.
+     *
+     * @param prompt Prompt to show to the user.
+     * @param defaultValue Default value.
+     * @return value given by the user
+     */
+    public static String askString(String prompt, String defaultValue) {
+        Scanner scanner = new Scanner(System.in);
+        System.out.print(prompt + (defaultValue != null ? " [" + defaultValue + "]" : "") + ": ");
+        String input = scanner.nextLine().trim();
+        return input.isEmpty() && defaultValue != null ? defaultValue : input;
+    }
+
+    /**
+     * Ask string value from user on command-line.
+     *
+     * @param prompt Prompt to show to the user.
+     * @return value given by the user or <code>null</code> if user did not provide any input.
+     */
+    public static String askString(String prompt) {
+        return askString(prompt, null);
+    }
 }