* {@code false} if they answered negatively,
* or {@code defaultValue} if input was empty
*/
- public static boolean askBoolean(final String prompt, final Boolean defaultValue) {
+ public static Boolean askBoolean(final String prompt, final Boolean defaultValue, boolean allowEmpty) {
while (true) {
- String finalPrompt = prompt;
- if (defaultValue != null){
- // Show the user something like "Continue? [Y/n]: " or "Continue? [y/N]: "
- finalPrompt = prompt + " [" + (defaultValue ? "Y/n" : "y/N") + "]: ";
- }
- String line = askString(finalPrompt);
+ // If we have a defaultValue, display it in brackets; otherwise display no default
+ String displayPrompt = prompt
+ + (defaultValue != null ? " [" + defaultValue + "]" : "")
+ + ": ";
+
+ // Read user input
+ System.out.print(displayPrompt);
+ String line = new Scanner(System.in).nextLine().trim();
+ // If user just pressed Enter:
+ if (line.isEmpty()){
+
+ // If a defaultValue was supplied, return it
+ if (defaultValue != null) {
+ return defaultValue;
+ }
- if ((defaultValue != null) && (line == null)) {
- return defaultValue;
+ if (allowEmpty) {
+ return null;
+ } else {
+ System.out.println("Input cannot be empty. Please enter y/yes/true or n/no/false.");
+ continue;
+ }
}
final String userInput = line.toLowerCase();
* @param max The maximum acceptable value (inclusive), or null if no upper bound
* @return A Float value that the user entered, or the defaultValue, or null if no defaultValue was given
*/
- public static Float askFloat(String prompt, Float defaultValue, Float min, Float max) {
+ public static Float askFloat(String prompt, Float defaultValue, Float min, Float max, boolean allowEmpty) {
while (true) {
// If we have a defaultValue, display it in brackets; otherwise display no default
String displayPrompt = prompt
System.out.print(displayPrompt);
String input = new Scanner(System.in).nextLine().trim();
- // If user just pressed Enter:
- if (input.isEmpty()) {
- // If a defaultValue was supplied, return it; else return null
- return defaultValue;
+ if (input.isEmpty()){
+ // If a defaultValue was supplied, return it
+ if (defaultValue != null) {
+ return defaultValue;
+ }
+
+ if (allowEmpty) {
+ return null;
+ } else {
+ System.out.println("Input cannot be empty. Please enter a valid float.");
+ continue;
+ }
}
+
// Parse float value
try {
float parsedValue = Float.parseFloat(input);
* @param max The maximum acceptable value (inclusive), or null if no upper bound
* @return A Long value that the user entered, or the defaultValue, or null if no defaultValue was given
*/
- public static Long askLong(String prompt, Long defaultValue, Long min, Long max) {
+ public static Long askLong(String prompt, Long defaultValue, Long min, Long max, boolean allowEmpty) {
while (true) {
// If we have a defaultValue, display it in brackets; otherwise display no default
String displayPrompt = prompt
System.out.print(displayPrompt);
String input = new Scanner(System.in).nextLine().trim();
- // If user just pressed Enter:
- if (input.isEmpty()) {
- // If a defaultValue was supplied, return it; else return null
- return defaultValue;
+ if (input.isEmpty()){
+
+ // If a defaultValue was supplied, return it
+ if (defaultValue != null) {
+ return defaultValue;
+ }
+
+ if (allowEmpty) {
+ return null;
+ } else {
+ System.out.println("Input cannot be empty. Please enter a valid long.");
+ continue;
+ }
}
// Parse long value
* <li>If {@code max} is not {@code null}, we enforce that the entered integer is {@code <= max}.</li>
* </ul>
*
- * @param prompt The prompt displayed to the user
- * @param defaultValue The default long if user simply presses Enter (may be null)
- * @param min The minimum acceptable value (inclusive), or null if no lower bound
- * @param max The maximum acceptable value (inclusive), or null if no upper bound
- * @return An integer value that the user entered, or the defaultValue, or null if no defaultValue was given
+ * @param prompt The prompt displayed to the user.
+ * @param defaultValue The default long if user simply presses Enter (may be null).
+ * @param min The minimum acceptable value (inclusive), or null if no lower bound.
+ * @param max The maximum acceptable value (inclusive), or null if no upper bound.
+ * @param allowEmpty If <code>true</code>, empty input is acceptable. Otherwise keep asking user.
+ * @return An integer value that the user entered, or the defaultValue, or null if no defaultValue was given.
*/
- public static Integer askInteger(String prompt, Integer defaultValue, Integer min, Integer max) {
+ public static Integer askInteger(String prompt, Integer defaultValue, Integer min, Integer max, boolean allowEmpty) {
while (true) {
// If we have a defaultValue, display it in brackets; otherwise display no default
String displayPrompt = prompt
String input = new Scanner(System.in).nextLine().trim();
// If user just pressed Enter:
- if (input.isEmpty()) {
- // If a defaultValue was supplied, return it; else return null
- return defaultValue;
+ if (input.isEmpty()){
+
+ // If a defaultValue was supplied, return it
+ if (defaultValue != null) {
+ return defaultValue;
+ }
+
+ if (allowEmpty) {
+ return null;
+ } else {
+ System.out.println("Input cannot be empty. Please enter a valid integer.");
+ continue;
+ }
}
// Parse long value
* @param defaultValue the value to return if the user provides no input
* @return the value typed by the user, or {@code defaultValue} if empty input
*/
- 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();
+ public static String askString(String prompt, String defaultValue, boolean allowEmpty) {
+ while (true) {
+ // If we have a defaultValue, display it in brackets; otherwise display no default
+ String displayPrompt = prompt
+ + (defaultValue != null ? " [" + defaultValue + "]" : "")
+ + ": ";
- // Return the default if user just pressed ENTER
- return input.isEmpty() ? defaultValue : input;
- }
+ // Read user input
+ System.out.print(displayPrompt);
+ String input = new Scanner(System.in).nextLine().trim();
+
+ if (input.isEmpty()){
+
+ // If a defaultValue was supplied, return it
+ if (defaultValue != null) {
+ return defaultValue;
+ }
+
+ if (allowEmpty) {
+ return null;
+ } else {
+ System.out.println("Input cannot be empty. Please enter a valid string.");
+ continue;
+ }
+ }
+
+ return input;
+ }
- /**
- * Asks the user for a string value using the specified prompt on the command line.
- * If the user presses ENTER without typing anything, {@code null} is returned.
- *
- * @param prompt the message to display to the user
- * @return the value typed by the user, or {@code null} if empty input
- */
- public static String askString(String prompt) {
- return askString(prompt, null);
}
}