-
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
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: ");
}
}
-
 Originally Posted by AdRock
would I use and if statement within the try block?
Why don't you try it and see?
-
Yes, and if negative, issue an error message and... continue;
eschew obfuscation
-
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
-
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
-
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
Code:
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)
{
-
 Originally Posted by KatyMc238
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.
Last edited by masher; 02-06-2007 at 07:47 PM.
Reason: clarification
-
You could do this:
Code:
//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...
Similar Threads
-
By rahulvasanth in forum Database
Replies: 1
Last Post: 01-26-2006, 10:48 PM
-
By borgfabr in forum .NET
Replies: 0
Last Post: 05-20-2005, 04:35 AM
-
By Warren in forum vb.announcements
Replies: 0
Last Post: 09-11-2002, 11:27 AM
-
By Narayan in forum VB Classic
Replies: 0
Last Post: 04-10-2001, 10:30 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks