I am trying to make a loop that prompts the user for a positive integer so it can be saved into int x.
If a negative or non-integer is entered it will prompt the user again.
It seems to work when a negative value is entered, but not when an exception is entered.
When an exception is entered it just endlessly prints "Please enter a positive integer." without allowing input.
What am I doing wrong? Please help guide me.
Thanks in advance.
int x = 0;
boolean firstLoop = false;
while (firstLoop == false)
{
firstLoop = true;
try
{
x = in.nextInt();
}
catch (InputMismatchException e)
{
System.out.println("Please enter a positive integer.");
firstLoop = false;
}
if (x < 0)
{
System.out.println("Please enter a positive integer.");
firstLoop = false;
}
}