Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java validation help needed

Status
Not open for further replies.

AdRock952

Technical User
Feb 6, 2004
16
GB
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: ");
}
}
 
Some ideas:

- Use code tags when posting code.
- You don't check if value is greater than zero

Cheers,
Dian
 
I couldn't find code tags anywhere on the page otherwise I would have done that

Underneath the validation I have this <b>while (packageWeight != 0) { more code }</b> which goes to the next part of the code if what is entered is any number but zero. Unfortunately negative numbers are still valid inout but I want to make it invalid
 
Your using an integer to compare to a double.

Possibly something like this :

Code:
          do
          {
             System.out.print("Enter a package weight (kg) or press 0 to exit: ");
             
             try
             {
                 //Assign the value entered from the keyboard to packageWeight
                packageWeight = new Double(keyboardInput.readLine()).doubleValue();           
             }
             catch (NumberFormatException e)
            {
             	
                packageWeight = 1.0; //push them back to the prompt
                
            }    
          } while (packageWeight > 0.0);

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
That will end if the user enters a negative value, you need to change the assignmet to -1.0 and change the comparison.

Cheers,
Dian
 
That will end if the user enters a negative value, you need to change the assignmet to -1.0 and change the comparison.
Sedj is spot on. == for float doesn't always work, and
while(packageWeight > 0.0) is false for 0. If he needs exactly 0, then he needs to check while(packageWeight > 0.0 && packageWeight < 0.0)[/red] becuase "is both naïve and dangerous to test two floating point values for strict equality" -Sun.

You may argue that he's also excluding negative numbers, but negative weight doesn't make sense as weight is not a vecotor. Personally, I'd consider negative an error condition.

"you need to change the assignmet to -1.0", why?

[plug=shameless]
[/plug]
 
OP wants the program to iterate until the user enters a positive number. Sedj's sollution will do that, but with negative numbers.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top