I have some validation that checks for numerical input and displays an error message if anything else is entered.
What I want is, if the user enters a negative number, an error message is displayed also and asks again for input until they enter a positive number.
Everything I try lets the user enter a negative number
Here is my vallidation code
boolean wrongValue = true; //
//Prompt user to enter a weight for the package
System.out.print("Enter a package weight (kg) or press 0 to exit: ");
//while the value given to packageWeight is wrong, keep doing this loop.
while(wrongValue)
{
try
{
//Assign the value entered from the keyboard to packageWeight
packageWeight = new Double(keyboardInput.readLine()).doubleValue();
//Set boolean variable wrongValue to false so it can escape the loop
wrongValue = false;
}
catch (NumberFormatException e)
{
//Prompt user to enter a weight for the package
System.out.print("Enter a package weight (kg) or press 0 to exit: ");
}
}
What I want is, if the user enters a negative number, an error message is displayed also and asks again for input until they enter a positive number.
Everything I try lets the user enter a negative number
Here is my vallidation code
boolean wrongValue = true; //
//Prompt user to enter a weight for the package
System.out.print("Enter a package weight (kg) or press 0 to exit: ");
//while the value given to packageWeight is wrong, keep doing this loop.
while(wrongValue)
{
try
{
//Assign the value entered from the keyboard to packageWeight
packageWeight = new Double(keyboardInput.readLine()).doubleValue();
//Set boolean variable wrongValue to false so it can escape the loop
wrongValue = false;
}
catch (NumberFormatException e)
{
//Prompt user to enter a weight for the package
System.out.print("Enter a package weight (kg) or press 0 to exit: ");
}
}