How to check is value entered is less than zero


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: How to check is value entered is less than zero

  1. #1
    Join Date
    Jan 2007
    Posts
    60

    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: ");
             	}
    }

  2. #2
    Join Date
    Jul 2005
    Posts
    78
    Quote Originally Posted by AdRock
    would I use and if statement within the try block?
    Why don't you try it and see?

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Yes, and if negative, issue an error message and... continue;
    eschew obfuscation

  4. #4
    Join Date
    Jan 2007
    Posts
    60
    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

  5. #5
    Join Date
    Nov 2006
    Posts
    13
    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

  6. #6
    Join Date
    Jan 2007
    Posts
    60
    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)
    		{

  7. #7
    Join Date
    Jul 2005
    Posts
    78
    Quote 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

  8. #8
    Join Date
    Jul 2005
    Posts
    78
    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

  1. conditional check in oracle
    By rahulvasanth in forum Database
    Replies: 1
    Last Post: 01-26-2006, 10:48 PM
  2. Spell check
    By borgfabr in forum .NET
    Replies: 0
    Last Post: 05-20-2005, 04:35 AM
  3. Replies: 0
    Last Post: 09-11-2002, 11:27 AM
  4. Check box isssue when bound to a database column
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center