Results 1 to 18 of 18
  1. #1
    kumarv75 is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Smile 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. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    830
    Rep Power
    6

    Default

    Paste the code till how much you tried.

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

  3. #3
    kumarv75 is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default

    Please do on ur own.............
    Last edited by kumarv75; 06-28-2010 at 11:44 AM.

  4. #4
    kumarv75 is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Smile 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. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

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

  6. #6
    kumarv75 is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default

    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. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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. #8
    kumarv75 is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Smile

    Quote Originally Posted by Norm View Post
    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. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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. #11
    kumarv75 is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Smile 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. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

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

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

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

  15. #15
    kumarv75 is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default

    I can post the program on request ..........................
    Last edited by kumarv75; 06-28-2010 at 11:46 AM.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

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

  17. #17
    kumarv75 is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Smile

    Quote Originally Posted by Norm View Post
    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. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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

  1. Java calculator problem ???
    By danielmessick in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2010, 06:53 PM
  2. Time Calculator Problem
    By ktisallred in forum New To Java
    Replies: 3
    Last Post: 09-22-2009, 07:15 PM
  3. NumberFormatException problem
    By bluebarca in forum New To Java
    Replies: 1
    Last Post: 02-06-2009, 07:39 AM
  4. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-19-2008, 11:58 PM
  5. Calculator Problem. Thanks for helping! ^^
    By clark_sandy in forum New To Java
    Replies: 3
    Last Post: 07-06-2008, 04:01 PM

Posting Permissions

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