Consider this, if userChoice
is not an integer:
Scanner choice = new Scanner(System.in);
int userChoice = choice.nextInt();
if(....){
.... //not important
}
else if(userChoice == null){
System.out.println("wrong input..");
System.exit(0);
}
Why cant I do this? And instead have to do:
import java.util.InputMismatchException;
..... //bunch of code
Scanner choice = new Scanner(System.in);
int userChoice;
try {
userChoise = choice.nextInt();
if(...){
..... //not important
}
}
catch(InputMismatchException e) {
System.out.println("wrong input..");
System.exit(0);
}
I thought it was so that if I inputed, say, a char
when it expected an int
it would simply return null
. So if I checked for null then it would be sufficient. So what am I missing/not understading?
So this is not a question regarding the Scanner library. If you want to boil it down it was that I did not know that a integer could not be null
. So the user who thought that this was duplicated, it might be. But it is certainly not a duplicate to the post that you suggested..