Results 1 to 6 of 6
Thread: NumberFormatException
- 10-09-2012, 01:39 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
NumberFormatException
Hi all,
I am experiencing a problem handling a NumberFormatException in my code.
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.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; }
Help?
-
Re: NumberFormatException
- 10-09-2012, 02:55 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: NumberFormatException
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 elsewhereJava 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(); } }
- 10-09-2012, 02:58 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: NumberFormatException
i have also tried this
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.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; }
-
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):
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.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 methodLast edited by Fubarable; 10-09-2012 at 03:08 AM.
- 10-09-2012, 03:28 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
NumberFormatException
By laxtrappa in forum New To JavaReplies: 6Last Post: 03-28-2012, 06:46 PM -
NumberFormatException
By Manfizy in forum New To JavaReplies: 13Last Post: 07-09-2009, 11:59 AM -
NumberFormatException raised
By venkatallu in forum New To JavaReplies: 1Last Post: 06-02-2009, 09:27 PM -
NumberFormatException problem
By bluebarca in forum New To JavaReplies: 1Last Post: 02-06-2009, 07:39 AM -
Error: NumberFormatException
By coco in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks