Reputation: 107
I'm wondering how do I go about validating this code so the input can only be an int and between a min and max? So far I can only stop the input being less than 1 and whatever maximum is used. But I cant seem to create a scenario where if the user inputs anything other than between the max and min (e.g. "AAA") it loops. I keep getting an Input Mismatch error. Any help would be greatly appreciated!
private static int getUserOption(String prompt, int max) {
int h;
do {
Scanner sc = new Scanner(System.in);
System.out.print(prompt);
h=sc.nextInt();
if(!sc.hasNextInt()) {
System.out.println("Invalid option. Try again!:");
}
} while(h<1 || h>max);
return h;
}
Upvotes: 0
Views: 1167
Reputation: 296
You Can try this Code !
Scanner sc = new Scanner(System.in);
int number;
do {
System.out.println("Please enter a valid number: ");
while (!sc.hasNextInt()) {
System.out.println("Error. Please enter a valid number: ");
sc.next();
}
number = sc.nextInt();
} while (!checkChoice(number));
private static boolean checkChoice(int choice){
if (choice <MIN || choice > MAX) { //Where MIN = 0 and MAX = 20
System.out.print("Error. ");
return false;
}
return true;
}
Ref. Validating input with while loop and scanner
Upvotes: 0
Reputation: 2204
I couldn't understand if you want to keep asking until the user types a valid answer or just ask once. The while block suggests to keep asking until is a valid input. Here is a small snippet that what you request. I suggest that you read some books, there a plenty of suggestions at SO.
public static class InvalidUserException extends Exception {
public InvalidUserException(String message) {
super(message);
}
public InvalidUserException(String message, Throwable cause) {
super(message, cause);
}
}
private static int getIntFromScanner(int max) throws InvalidUserException {
int nextInt = 0;
Scanner sc = new Scanner(System.in);
try {
nextInt = sc.nextInt();
} catch (InputMismatchException e) {
throw new InvalidUserException("Input must be a valid Integer", e);
}
if (nextInt > max) {
throw new InvalidUserException(
"Input is bigger than allowed! Max: " + max + " Input: "
+ nextInt);
}
return nextInt;
}
public static int getUserOption(String prompt, int max) {
System.out.println(prompt);
do {
try {
return getIntFromScanner(max);
} catch (InvalidUserException e) {
System.out.println("Invalid option. Try again ("
+ e.getMessage() + ")");
}
} while (true);
}
public static void main(String[] args) {
int userOption = getUserOption("Gimme less than or equal 6!", 6);
System.out.println("You gave me " + userOption);
}
Upvotes: 0
Reputation: 4736
a few points:
1. to check if the input is less than a min value just add int min
to the method signature.
2. to check if input is an int
, catch InputMismatchException
.
The revised code would be:
private static int getUserOption(String prompt, int max, int min) {
int h;
do {
Scanner sc = new Scanner(System.in);
System.out.print(prompt);
try {
h=sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("Invalid option. Try again!:");
}
} while(h<min || h>max);
return h;
}
Upvotes: 0
Reputation: 1695
The loop is breaking because the nextInt() method is throwing an exception, which terminates the method early.
One option would be to use a try / catch block to trap the exception:
private static int getUserOption(String prompt, int max) {
int h = 0;
do {
Scanner sc = new Scanner(System.in);
System.out.print(prompt);
try {
h=sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("Invalid option. Try again!:");
}
}while(h<1 || h>max);
return h;
}
Upvotes: 1