the user has to enter 3 integers into a list. im trying to make a try catch block to filer out if the user inputs anything other than an integer.
i inserted the try catch block inside the for loop, the problem is even if the input is not an integer my code scans it and only prints the "enter an integer:" 3 times. how to fix that?
THIS IS MY CODE
for(int i = 0; i<3; i++){
try{
//some code
}catch(Exception e){}
}
i would like the code to go like this:
if the user enters an integer, they only need to input 2 integers if the user enters a character, the code will prompt an error and will loop back to ask for an integer (2 integers only because the user already entered 1 integer)
enter an integer: 1
enter an integer: z
the input is invalid, please try again.
enter an integer: 2
enter an integer: ac
the input is invalid, please try again.
enter an integer: 3