Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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]
# 1 Re: How to check is value entered is less than zero
would I use and if statement within the try block?

Why don't you try it and see?
masher at 2007-11-11 22:31:37 >
# 2 Re: How to check is value entered is less than zero
Yes, and if negative, issue an error message and... continue; :)
sjalle at 2007-11-11 22:32:42 >
# 3 Re: How to check is value entered is less than zero
I tried using an If statment within the try block with an error message but it is still going onto the next part even though wrongValue is false
AdRock at 2007-11-11 22:33:41 >
# 4 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
KatyMc238 at 2007-11-11 22:34:46 >
# 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 >