public static void main(String[] args) {
int age=0 ;
while(age == 0) {
System.out.println("How old are you");
age = checkValidAge();
}
}
public static int checkValidAge() {
try {
return userInput.nextInt();
}
catch(InputMismatchException e) {
userInput.next();
System.out.println("Enter a interger");
return 0;
}
}
}
Started learning java recently(complete beginner).
So I am bit confused, when I input numbers this cause loop to break and when I enter characters loop dont break so isn't meant to be other way around.
Just wanted to create something that catches the error and lets you try again.