Hi I´m newbie in programming and I have this problem. I want to get input from user , using scanner. Program is supposed to try if input is valid, then call a function to do the job. Problem is I want program to repeat aking for input from user, if input is not valid. So I have try block in a while loop. The problem is on the first iteration of while loop everything is ok but when I insert invalid input and while loop is forced to iterate second time, try block is not executed and boolean which is condition of while loop is not set to false. So while loop runs for ever. Plz be kind.
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
boolean isRunning = true;
int input;
while(isRunning) {
System.out.println("insert a number:");
try {
input = scanner.nextInt();
inputToString(input);
isRunning = false;
} catch(InputMismatchException e) {
System.out.println("input musi byt cele cislo");
isRunning = true;
}
}
}
public static void inputToString(int input) {
System.out.println(input);
}