Results 1 to 18 of 18
 06182010, 09:53 AM #1Member
 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
 06182010, 09:56 AM #2
Paste the code till how much you tried.
Put printStackTrace() and find out where exactly you got the error.Ramya:cool:
 06182010, 10:12 AM #3Member
 Join Date
 Jun 2010
 Posts
 12
 Rep Power
 0
Please do on ur own.............
Last edited by kumarv75; 06282010 at 11:44 AM.
 06192010, 12:17 PM #4Member
 Join Date
 Jun 2010
 Posts
 12
 Rep Power
 0
cal
[code]
import javax.microedition.midlet.MIDlet;
import com.sun.lwuit.Button;
iLast edited by kumarv75; 06282010 at 11:45 AM. Reason: Moderator Edit: Code tags added
 06192010, 12:52 PM #5
Can you post the full text of the error message and show what the input was that causes the error?
 06212010, 06:40 AM #6Member
 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
 06212010, 12:39 PM #7if we add 1+2+3 the answer we get is 36
 06222010, 07:08 AM #8Member
 Join Date
 Jun 2010
 Posts
 12
 Rep Power
 0
 06222010, 12:40 PM #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));
 06222010, 03:53 PM #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.
 06232010, 10:18 AM #11Member
 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
 06232010, 11:16 AM #12Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 9
 06232010, 12:33 PM #13want the same calc to work with the keypad keys
 06232010, 02:23 PM #14
If you convert the logic part of the code to Swing or AWT then others can help. Not all of us have ME
 06252010, 01:39 PM #15Member
 Join Date
 Jun 2010
 Posts
 12
 Rep Power
 0
I can post the program on request ..........................
Last edited by kumarv75; 06282010 at 11:46 AM.
 06252010, 02:35 PM #16
Use code tags when posting large pieces of code.
Do you have a question?
 06282010, 08:08 AM #17Member
 Join Date
 Jun 2010
 Posts
 12
 Rep Power
 0
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
 06282010, 12:47 PM #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
Similar Threads

Java calculator problem ???
By danielmessick in forum Advanced JavaReplies: 2Last Post: 03132010, 07:53 PM 
Time Calculator Problem
By ktisallred in forum New To JavaReplies: 3Last Post: 09222009, 07:15 PM 
NumberFormatException problem
By bluebarca in forum New To JavaReplies: 1Last Post: 02062009, 08:39 AM 
Problem in Calculator implementation using Stack
By realahmed8 in forum New To JavaReplies: 1Last Post: 12202008, 12:58 AM 
Calculator Problem. Thanks for helping! ^^
By clark_sandy in forum New To JavaReplies: 3Last Post: 07062008, 04:01 PM
Bookmarks