Skip to content

Instantly share code, notes, and snippets.

@intelliapps-io
Last active October 9, 2020 15:27
Show Gist options
  • Save intelliapps-io/3d9e0773586927b910379ca542a179f8 to your computer and use it in GitHub Desktop.
Save intelliapps-io/3d9e0773586927b910379ca542a179f8 to your computer and use it in GitHub Desktop.
Java Scanner Input Helpers
/**
* Created: 7/19/2019
* Updated: 10/9/2020
* Uploaded: 10/5/2020
* Author: Jared Moore
* Gist: https://gist.github.com/intelliapps-io/3d9e0773586927b910379ca542a179f8
*/
import java.util.Scanner;
interface StringValidator {
boolean test(String input);
}
interface IntValidator {
boolean test(int input);
}
interface DoubleValidator {
boolean test(double input);
}
public class InputHelpers {
public static void log(String msg) {
System.out.println(msg);
}
/**
* Get Integer Input
*
* @param validator lambda (input) -> boolean
* @param errorMessage String
* @return int
*/
public static int getIntInput(IntValidator validator, String errorMessage) {
Scanner keyboard = new Scanner(System.in);
int parsedInput = 0;
boolean inputValidated = false;
do {
String input = keyboard.nextLine();
try {
parsedInput = Integer.parseInt(input);
if (validator.test(parsedInput))
inputValidated = true;
else
throw new Exception("");
} catch (Exception error) {
log("\nError, \"" + input + "\" is invalid.\n" + errorMessage);
}
} while (!inputValidated);
return parsedInput;
}
public static int getIntInput() {
Scanner keyboard = new Scanner(System.in);
int parsedInput = 0;
boolean inputValidated = false;
do {
String input = keyboard.nextLine();
try {
parsedInput = Integer.parseInt(input);
inputValidated = true;
} catch (Exception error) {
log("\nError, \"" + input + "\" is not an integer.\n");
}
} while (!inputValidated);
return parsedInput;
}
/**
* Get Double Input
*
* @param validator lambda (input) -> boolean
* @param errorMessage String
* @return double
*/
public static double getDoubleInput(DoubleValidator validator, String errorMessage) {
Scanner keyboard = new Scanner(System.in);
double parsedInput = 0;
boolean inputValidated = false;
do {
String input = keyboard.nextLine();
try {
parsedInput = Double.parseDouble(input);
if (validator.test(parsedInput))
inputValidated = true;
else
throw new Exception("");
} catch (Exception error) {
log("\nError, \"" + input + "\" is invalid.\n" + errorMessage);
}
} while (!inputValidated);
return parsedInput;
}
public static double getDoubleInput() {
Scanner keyboard = new Scanner(System.in);
double parsedInput = 0;
boolean inputValidated = false;
do {
String input = keyboard.nextLine();
try {
parsedInput = Double.parseDouble(input);
inputValidated = true;
} catch (Exception error) {
log("\nError, \"" + input + "\" is not a number.\n");
}
} while (!inputValidated);
return parsedInput;
}
/**
* Get String Input
*
* @param validator lambda (input) -> boolean
* @param errorMessage String
* @return String
*/
public static String getStringInput(StringValidator validator, String errorMessage) {
Scanner keyboard = new Scanner(System.in);
String parsedInput = "";
boolean inputValidated = false;
do {
String input = keyboard.nextLine();
try {
if (validator.test(input)) {
inputValidated = true;
parsedInput = input;
}
else
throw new Exception("");
} catch (Exception error) {
log("\nError, \"" + input + "\" is invalid.\n" + errorMessage);
}
} while (!inputValidated);
return parsedInput;
}
public static String getStringInput() {
Scanner keyboard = new Scanner(System.in);
return keyboard.nextLine();
}
/**
* Get Boolean Input
*
* @return boolean
*/
public static boolean getBooleanInput(String errorMessage) {
Scanner keyboard = new Scanner(System.in);
boolean parsedInput = false;
boolean inputValidated = false;
do {
String input = keyboard.nextLine().trim().toLowerCase();
if (input.equals("true") || input.equals("t") || input.equals("yes") || input.equals("y")) {
inputValidated = true;
parsedInput = true;
} else if (input.equals("false") || input.equals("f") || input.equals("no") || input.equals("n"))
inputValidated = true;
else
log("\nError, \"" + input + "\" is invalid! Enter only \"true\" or \"false\"\n" + errorMessage);
} while (!inputValidated);
return parsedInput;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment