Results 1 to 6 of 6
  1. #1
    BurntCandy is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default NumberFormatException

    Hi all,
    I am experiencing a problem handling a NumberFormatException in my code.
    Java Code:
    double getPrincipal(){
    		double test = Double.parseDouble(JOptionPane.showInputDialog(null, "enter a principal between 1 and 500,000"));
    		if(test > 0 && test < 500001){
    		} else this.getPrincipal();
    		return test;
    	}
    If someone types a string instead of a number into the dialog box I get a number format exception. Is there any way to check if Double.parseDouble worlds before using it. I have tried a try/catch but that messed everything up because the variables were not local.

    Help?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: NumberFormatException

    Quote Originally Posted by BurntCandy View Post
    ...I have tried a try/catch but that messed everything up because the variables were not local.
    That's the solution. You must fix the errors that you're now seeing. If you need help, show the code and errors to us.

  3. #3
    BurntCandy is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: NumberFormatException

    Java Code:
    double getPrincipal(){
    		try{
    		double test = Double.parseDouble(JOptionPane.showInputDialog(null, "enter a principal between 1 and 500,000"));
    		if(test > 0 && test < 500001){
    		} else this.getPrincipal();
    		return test;
    		}catch (NumberFormatException bad){
    			this.getPrincipal();
    		}
    		
    	}
    It is now giving me an error saying that I need to add a return or change the return type to void but I only want it to return once I get a value in between 0 and 500000 so I don't want to add one elsewhere

  4. #4
    BurntCandy is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: NumberFormatException

    i have also tried this
    Java Code:
    double getPrincipal(){
    		double test; 
    		try{
    			Double.parseDouble(JOptionPane.showInputDialog(null, "enter a principal between 1 and 500,000"));
    		} catch (NumberFormatException a){
    			this.getPrincipal();
    		}
    		if(test > 0 && test < 500001){
    		} else this.getPrincipal();
    		return test;
    		
    	}
    but it tells me that I need to initialize test. When I initialized test to a value it ended up only returning the value it was initialized to.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: NumberFormatException

    If there were my method, I don't think I'd try to solve it using recursion (having the method call itself). Instead I'd use a while loop, and only exit the loop when a decent value has been entered. something like (in pseudocode):

    Java Code:
    method getPrinciple 
      declare a double dummy variable, test, and set it to a default value of 0.0.
      declare a boolean testInvalid and set to true
      while testInvalid 
        try block
          get input and parse it to double
          // if I'm at this line, the parse was successful!
          check if test is in the valid range
          if so, return test
          if not, tell the user that the number entered was out of range to try again.
        catch for NumberFormatException
          tell the user that they need to enter a valid number.
        end of try/catch block
      end while loop
    end of method
    Actually you don't even need a boolean variable here and can simply use while (true) since my loop ends by returning a valid value from the method from within the while loop.
    Last edited by Fubarable; 10-09-2012 at 04:08 AM.

  6. #6
    BurntCandy is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: NumberFormatException

    ahh, Ive been beating my head against the wall with this but had not thought of changing the overall structure
    Thanks!

Similar Threads

  1. NumberFormatException
    By laxtrappa in forum New To Java
    Replies: 6
    Last Post: 03-28-2012, 07:46 PM
  2. NumberFormatException
    By Manfizy in forum New To Java
    Replies: 13
    Last Post: 07-09-2009, 12:59 PM
  3. NumberFormatException raised
    By venkatallu in forum New To Java
    Replies: 1
    Last Post: 06-02-2009, 10:27 PM
  4. NumberFormatException problem
    By bluebarca in forum New To Java
    Replies: 1
    Last Post: 02-06-2009, 08:39 AM
  5. Error: NumberFormatException
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:41 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
  •