# Thread: Problem with NumberFormatException in calculator

1. Member
Join Date
Jun 2010
Posts
12
Rep Power
0

## Problem with NumberFormatException in calculator

Hi I am doing a calculator program in lwuit like the one that v have in our windows calculator .........................I have some problems in my code......... I have did the initial coding for forms buttons and have even made the operators -,+,*,/ work but the problem now I am facing is continues pressing of any operator I am not getting the result as u get in win calculator ,so results cum only if = button I have added a if condition for = wherein if it is pressed the value will be shown now I have added a or operator to it

if (operand.equals("=")|| (operand.equals(operation))) and while executing I get a java.lang.NumberFormatException but continuing the execption I am able get the result when any operator is pressed so can u help me in solving me this execption ..........................I have one more problem say if I calculate 1+1+1 the result that cums is 13 ie it takes the previous value entered also so for this also I need a solution if u have any suggestion please feel free and tell me I would be pleased................
:) Thank you

2. Paste the code till how much you tried.

Put printStackTrace() and find out where exactly you got the error.

3. Member
Join Date
Jun 2010
Posts
12
Rep Power
0
Please do on ur own.............
Last edited by kumarv75; 06-28-2010 at 11:44 AM.

4. Member
Join Date
Jun 2010
Posts
12
Rep Power
0

## cal

[code]
import javax.microedition.midlet.MIDlet;
import com.sun.lwuit.Button;
i
Last edited by kumarv75; 06-28-2010 at 11:45 AM. Reason: Moderator Edit: Code tags added

5. Can you post the full text of the error message and show what the input was that causes the error?

6. Member
Join Date
Jun 2010
Posts
12
Rep Power
0
hi

The problem that I face now is continues numbers say if we add 1+2+3 the answer we get is 36 where the previous valueentered is also taken

7. if we add 1+2+3 the answer we get is 36
Have you tried debugging the code by adding println()s to show how the variables change and what the values are thru the code?

8. Member
Join Date
Jun 2010
Posts
12
Rep Power
0
Originally Posted by Norm
Have you tried debugging the code by adding println()s to show how the variables change and what the values are thru the code?

S I even tried with SOP's to find whr the error is ..................

9. What did you see in the printed output? You need to analyze it to see where your logic is wrong.

Not many people have the ME dev kit. If you'd rewrite the code to use Swing then others could compile and test the code to see what's wrong with your logic.

Can you explain how the calculator is supposed to work. Does the user type in the expression: 1+2+3 and press enter? Or ?

Some comments:
txt.setText(" ");
txt.setText(String.valueOf(result));
Why do you set the value of txt 2 times?

10. Some more ideas:
Disable the text field so user can't enter data
Enable/disable buttons so the user can't use them in the wrong order.
Do this by have groups/arrays of buttons that should be enabled/disabled at the same time and a method to do the setting.
For example groups: the numbers, the operators, the equal sign.
Have different actionListeners for each group of buttons.

Then in your code start with only enabling the numbers.
After the first number then enable operators.
After an operator, disable operators
After next number, enable equals.

This will force user to enter in correct order.

11. Member
Join Date
Jun 2010
Posts
12
Rep Power
0

## Hi

Hi

I want the same calc to work with the keypad keys for that keyListener should be used but I don't know how to use that

12. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10

13. want the same calc to work with the keypad keys
You need to get the logic working before adding new features.

14. If you convert the logic part of the code to Swing or AWT then others can help. Not all of us have ME

15. Member
Join Date
Jun 2010
Posts
12
Rep Power
0
I can post the program on request ..........................
Last edited by kumarv75; 06-28-2010 at 11:46 AM.

16. Use code tags when posting large pieces of code.
Do you have a question?

17. Member
Join Date
Jun 2010
Posts
12
Rep Power
0
Originally Posted by Norm
If you convert the logic part of the code to Swing or AWT then others can help. Not all of us have ME

I have a question whnever I do any operation like 6*. or 5+. and so on I get a number formating execption and also I am able to enter values like 6.1.2.3. which should not come in a calculator another bug I face is values get append to the result say if I calculate 6+2 and press = gives 8 when I press a new avlue it gets appended to 8 like if I enter 5 then it comes as 85......Can u help me in this prob that I am facing

18. Your code must be sensitive to what the next allowable input is. This is call its 'state'.
For example initially only a number or a unary - is allowed.
Once you have a number, then only numbers, . or an operator is allowed
Once you get a . no more . are allowed and only numbers are allowed
Once you get an operator then only numbers or . are allowed.
once you get a number then only number, . or = are allowed
If you get a . then only number is allowed.

Some of this can be done by enabling/disabling the input buttons. Some has to be done by remembering the state with booleans and tests in the code.

Again:
If you convert the logic part of the code to Swing or AWT then others can help. Not all of us have ME

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•