mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-26 12:51:29 -08:00
More robust validation
This commit is contained in:
@@ -598,17 +598,33 @@ public class CivilWar {
|
||||
}
|
||||
|
||||
private static String inputString(Predicate<String> validator, String reminder) {
|
||||
var terminalInput = new Scanner(System.in);
|
||||
|
||||
while (true) {
|
||||
var input = terminalInput.nextLine();
|
||||
if (validator.test(input)) {
|
||||
return input;
|
||||
try {
|
||||
var input = new Scanner(System.in).nextLine();
|
||||
if (validator.test(input)) {
|
||||
return input;
|
||||
}
|
||||
} catch (InputMismatchException e) {
|
||||
// Ignore
|
||||
}
|
||||
System.out.println(reminder);
|
||||
}
|
||||
}
|
||||
|
||||
private static int inputInt(Predicate<Integer> validator, Function<Integer, String> reminder) {
|
||||
while (true) {
|
||||
try {
|
||||
var input = new Scanner(System.in).nextInt();
|
||||
if (validator.test(input)) {
|
||||
return input;
|
||||
}
|
||||
System.out.println(reminder.apply(input));
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println(reminder.apply(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isYes(String s) {
|
||||
if (s == null) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user