The algorithm should take in 3 integers to an ArrayList. If the input is not an integer, then there should be a prompt. When I execute my code the catch
clause is e
If inputNum = input.nextInt();
cannot be fit into an int and a InputMismatchException
is raised, the input of the Scanner
is not consumed.
So after the catch, it loops and it goes again here :
inputNum = input.nextInt();
with exactly the same content in the input.
So you should execute input.nextLine();
in the catch statement to discard the current input and allow a new input from the user.
Besides it makes more sense to catch InputMismatchException
rather than Exception
as other exception with no relation with a mismatch could occur and it would not be useful to display to the user "invalid number "
if it is not the issue :
catch (InputMismatchException e){
System.out.println("invalid number ");
input.nextLine();
}