Ankur Teotia
Ankur Teotia

Reputation: 227

why does this does throw a number format exception

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.

Upvotes: -3

Views: 168

Answers (1)

Juned Ahsan
Juned Ahsan

Reputation: 68715

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

Upvotes: 1

Related Questions