-3

its basically a button event that does multiplication

  mul.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {


                if(Sans == null){
                    temp = text;
                }else {
                    temp = Sans ;
                }
                text = "";

                equal = "mul" ;

                textArea.setText("*");
            }
        });
           this is the action that happens when pressing equal button
                 ans.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {

                if(equal == "mul"){
                    double a = Double.parseDouble(text);
                    double b = Double.parseDouble(temp);
                    double ans = b*a;
                    String Sans = String.valueOf(ans);
                    textArea.setText(Sans);
                    text = "" ;
                }
                    }

this code gives the result for the first multiplication but gives a numberformatexception on successive multiplication the same code works fine doing a divide operation.

Obl Tobl
  • 5,604
  • 8
  • 41
  • 65
Ankur Teotia
  • 227
  • 1
  • 10
  • 1
    Bug in your code: Do not use `==` to compare strings. `if(equal == "mul")` is wrong; use `if (equal.equals("mul"))` instead. – Jesper May 23 '13 at 06:54
  • NFE is thrown when you try to parse something that is not a number. If you'll add the full stacktrace we could help you more. – BobTheBuilder May 23 '13 at 06:55
  • Do some debugging. Either use your debugger in the IDE and set a breakpoint on the first Double.parseDouble line, or add some System.out.printlns (I think you're interested in the value of text and temp, then you'll see what is wrong). – Aleksander Blomskøld May 23 '13 at 06:56
  • If you added the code related to setting the values of `Sans`. `text`, `temp`, that would help as well. – gkalpak May 23 '13 at 06:56
  • these are their initial values String text = "0" ; String temp = "" ; String equal = ""; String Sans = ""; – Ankur Teotia May 23 '13 at 07:24

1 Answers1

1

Update your if condition from

if(equal == "mul")

to

if(equal.equals("mul"))

And learn more about string comparisions. Related post https://stackoverflow.com/questions/767372/java-string-equals-versus

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • @AnkurTeotia print the numbers before you assign them to a double. If a number string is not a valid number for example "7s"/"7x" or anything then you will get numberformatexception while doing Double.parseDouble. So make sure your number string is good to be converted to double. – Juned Ahsan May 23 '13 at 07:19