• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why am I getting NumberFormatException?

 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me why I'm getting a NumberFormatException, coz for the life of me I can't see whats wrong with it.
It's basically an applet that allows you to enter 2 numbers which when you click on 1 of 4 radio buttons( +, -, *, / ) and then click calculate, displays the answer in a JTextField. I've used the method getText() to retrieve the inputted strings, then converted these to ints, did the calculation, then converted them back to a String in order to display in the result JTextField.(Is it possible to leave the result as an int to display in result field?)

Any help would really be appreciated
 
Abhilash Vasudevan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems you are parsing the textfields for the val1 and val2 in the init(). But at initialising time you will not have anything in there.
This has to be done once you have typed in some number in there and clicked the calculate button.
So include the val1,val2 assigning statements inside your action performed method and then it should work
Regards
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhilash,
Iv'e done as you said and moved the assignments to here:

So, I now get the GUI displaying propery. I can enter 2 numbers into the textfields and click on a radio button such as'+' to add the 2 numbers together. I then click the 'calculate' button but nothing appears in the result textfield.
Is there something wrong in the if statements to do with setText()???
 
Nicholas Turner
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do a repaint()
If your curious about the values do debugging like so
System.err.println("val1 = " + val1);
then open the Java console and check the values out
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic