| Author |
NumberFormatException
|
Chris Garrison
Greenhorn
Joined: May 17, 2002
Posts: 19
|
|
I'm getting the following error -- Exception occurred during event dispatching: java.lang.NumberFormatException. I'm trying to parse a string to an int from a JTextField but I'm having no luck. In the documentation it said the String may not be in the correct format, but I'm not sure what that means? Thanks in advance for your help.
|
SCJP2
|
 |
Paul Stevens
Ranch Hand
Joined: May 17, 2001
Posts: 2823
|
|
|
It means whatever you are passing is not numbers. Have you looked at the whole number example in the Sun tutorial. It will prevent nonnumbers from being entered in the JTextField.
|
 |
Chris Garrison
Ranch Hand
Joined: Jun 13, 2002
Posts: 44
|
|
Well... I did see that but I didn't think I needed anything that slick. I mean - I know it's not a number - its a String, right??? It's my first time using swing. Basically what I want to do (I'm thinking in terms of AWT) is to get the contents of a JTextField (which is a String as far as I know), and then parse that (Integer.parseInt(string s)) to an int. I get a NumberFormatException when I try to parse it. ?? Do I have to use whole number class or is there a way to parse whatever it is in the JTextField to an int??? Is Swing that different/counterintuitive? Does it have something to do with the "Exception during Event Dispatching"??? I don't know what that is??? Thanks [ June 13, 2002: Message edited by: Chris Garrison11 ] [ June 13, 2002: Message edited by: Chris Garrison11 ] [ June 13, 2002: Message edited by: Chris Garrison11 ]
|
May the force be with you, always.
|
 |
Chris Powers
Greenhorn
Joined: Jun 13, 2002
Posts: 1
|
|
I use the following code to parse from a JTextField: This is a little tricky. I had to set the JTextField to "" to start (if you have a default int value that works, use it). Then I had to use if statements in case "" was the only text in the field. I was only able to use the above code when there was already a String in the field (i.e., in the else statement). In places, I had to put the whole if-else structure in a try-catch block in case the entered text wasn't numerical, then send a message to the user in one of the Swing Components. NumberFormatException is thrown whenever your program checks the JTextField and doesn't find numerical text ("1234567890"). Good luck!
|
 |
Chris Garrison
Greenhorn
Joined: May 17, 2002
Posts: 19
|
|
Thanks for your help. It was a problem not related to JTextField. It was a problem related to my brain.
|
 |
 |
|
|
subject: NumberFormatException
|
|
|