• Post Reply Bookmark Topic Watch Topic
  • New Topic

NumberFormatException  RSS feed

 
Chris Garrison
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Chris Powers
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
It was a problem not related to JTextField. It was a problem related to my brain.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!