How to check is value entered is less than zero
I have some code that validate suser input and checks that they enter 0 and over but i need something to check that the user doesn't enter -1 or something. Currently the code would let them carry on and i don't want that.
How would i check for this input...would I use and if statement within the try block?
Here is the 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: ");
}
}
[1218 byte] By [
AdRock] at [2007-11-11 10:19:28]

# 5 Re: How to check is value entered is less than zero
At the moment if the user enters anything but a positive number or 0 then it catches a number format exception, but because -1 is a legitimate number it exits the loop and carries on as normal.
Anthing less than 0 can't have a weight so I want to prevent the user entering anything less than 0
I have tried this but it still doesn't catch anything less than 0
boolean wrongValue = true; //
System.out.print("Enter a package weight (kg) or press 0 to exit: ");
while(wrongValue)
{
try
{
packageWeight = new Double(keyboardInput.readLine()).doubleValue();
wrongValue = false;
}
catch (NumberFormatException e)
{
System.out.print("Enter a package weight (kg) or press 0 to exit: ");
}
}
while (packageWeight != 0 && packageWeight <0)
{
AdRock at 2007-11-11 22:35:39 >

# 6 Re: How to check is value entered is less than zero
Im not sure but does while(wrongvalue){.... do anything at all?? Surely that condition just means that "while wrongvalue has a value"..
If you put while(wrongvalue==false){... that might work
It works. The expression in the brackets after 'while' is evaluated to a boolean.
So, while(num > 0) will continue to loop while num is greater than 0, as num > 0 will be true. As wrongValue is already a boolean, there is no need to have a equality or other operator, as the reassignment of wrongValue to true/false occurs inside the loop.
masher at 2007-11-11 22:36:49 >

# 7 Re: How to check is value entered is less than zero
You could do this:
//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();
if(packageWeight < 0)
{
throw new NumberFormatException();// or you could define your own exception...
}
else
{
//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: ");
}
}
I don't know how legitimate it is to throw exceptions, but you could always define your own NumberLessThanZeroException class, and throw that...
masher at 2007-11-11 22:37:47 >
