Reputation: 115
The following code is in a while loop which then houses a switch as such:
System.out.println("Enter in a selection.");
System.out.println("Enter \"1\" for a default selection of die");
System.out.println("Enter \"2\" for a custom number of sides.");
//try the input to see if its an integer
try {
selection = sc.nextInt();
} catch (NumberFormatException e){
System.out.print("Your selection can only be an integer!");
}
switch (selection){
case1:
...
break;
case2:
...
break;
default:
...
//yell at them
continue;
}
I already have a default selection in the switch so that if a user enters in an invalid number like 4 (since there are only 2 cases) that it brings them back to the beginning of the loop. So, the issue is handling the exception. The exception does not get handled with the following above code and I don't know why. The try is housing the offending code.
As always, please ask for clarification if needed. Thanks.
Upvotes: 1
Views: 16497
Reputation: 51030
You should continue
on the exception as there is no point in going ahead do the switch
on the selection
if the exception occurred.
} catch (InputMismatchException e){
System.out.print("Your selection can only be an integer!");
sc.nextLine();
continue;
}
The way you have it right now, if the exception were to occur you would be switching the old stale value held by selection
.
But the main problem in your code is that the nextInt()
method doesn't throw the NumberFormatException
, instead it throws the InputMismatchException
.
So you are trying to catch the wrong exception.
Upvotes: 1
Reputation: 34367
Scanner#nextInt() doesn't throw NumberFormatException
Scans the next token of the input as an int. An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix) , where radix< is the default radix of this scanner.
return the int scanned from the input
throws InputMismatchException
if the next token does not match the <i>Integer</i>
regular expression, or is out of range
throws NoSuchElementException if input is exhausted
throws IllegalStateException if this scanner is closed
Your exception handling seems right, with only issue that you are catching wrong exception. Please catch InputMismatchException
and other two.
e.g. below:
try {
selection = sc.nextInt();
} catch (InputMismatchException e){
System.out.print("Your selection can only be an integer!");
}
Also you may want to put the above code in a while loop as below:
boolean validInput = false;
while(!validInput){
try {
selection = sc.nextInt();
validInput = true;
} catch (InputMismatchException e){
System.out.print("Your selection can only be an integer!");
}
}
This will repeatedly ask for input until a number is entered.
Upvotes: 1
Reputation: 897
If you want to really handle the exception in the 'switch' statement, you need to expand the scope of the try-catch block:
System.out.println("Enter in a selection.");
System.out.println("Enter \"1\" for a default selection of die");
System.out.println("Enter \"2\" for a custom number of sides.");
//try the input to see if its an integer
try {
selection = sc.nextInt();
switch (selection){
case1:
...
break;
case2:
...
break;
default:
...
//yell at them
throw new NumberFormatException("Yelling message");
continue;
}
} catch (NumberFormatException e){
System.out.print("Your selection can only be an integer!");
}
Upvotes: 0
Reputation: 121669
Personally, I would put your input (and the try/catch block that goes with it) in its own, separate method. Return a boolean (true = valid integer) or a value that's out of range (perhaps "-1", depending on your program).
IMHO...
Upvotes: 1
Reputation: 46193
In the case of a Scanner
, you can use the hasNextInt()
method (and equivalents for other data types) rather than crashing and burning via exception:
while (some_condition) {
System.out.println("Enter in a selection.");
System.out.println("Enter \"1\" for a default selection of die");
System.out.println("Enter \"2\" for a custom number of sides.");
//try the input to see if its an integer
if (!sc.hasNextInt()) {
System.out.println("You must enter an integer!");
continue;
}
selection = sc.nextInt();
switch (selection){
case 1:
...
break;
case 2:
...
break;
default:
...
//yell at them
continue;
}
}
As Bhesh Gurung said, if you're in a loop, you should just use continue
to go back to the beginning, like you do in the default
case.
Upvotes: 0
Reputation: 13356
The reason exception is not handled when you enter 4
is that 4
is a valid integer and nextInt()
will not raise NumberFormatException
exception.
You'd better enclose the switch
in the try
block.
Upvotes: 1