Java Calculator minus error

I have written this code of calculator.

However, at runtime, if I use the minus button, it becomes plus.
I had tried to delete the code of that plus/minus button but the problem still there.

I have already tried everything, the only useful thing that I found is that the “-” string did not register.

Code:

public class Calculator extends javax.swing.JFrame {
    static String OP = "",
              operations = "[+|-|x|/|0|-|.]",
              anw = "noInputYet";
    static float result,
             firstNumber = 0, 
             secondNumber = 0;
    static double prNumber = 0 ;
    static int numberClick = 0;



    private void bt1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // Button 1 

        if (calScreen.getText().matches(operations)) {
            calScreen.setText("1");}

        else if (result != 0) {
           calScreen.setText("1");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "1");
        }
    }                                   

    private void bt2ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 2 
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("2");}

        else if (result != 0) {
           calScreen.setText("2");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "2");
        }
    }                                   

    private void bt3ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 3 
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("3");}

        else if (result != 0) {
           calScreen.setText("3");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "3");
        }
    }                                   

    private void bt9ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 9
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("9");}

        else if (result != 0) {
           calScreen.setText("9");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "9");
        }
    }                                   

    private void bt8ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 8
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("8");}

        else if (result != 0) {
           calScreen.setText("8");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "8");
        }
    }                                   

    private void bt7ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 7
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("7");}

        else if (result != 0) {
           calScreen.setText("7");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "7");
        }
    }                                   

    private void bt4ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 4
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("4");}

        else if (result != 0) {
           calScreen.setText("4");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "4");
        }
    }                                   

    private void bt5ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 5
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("5");}

        else if (result != 0) {
           calScreen.setText("5");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "5");
        }
    }                                   

    private void bt6ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 6
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("6");}

        else if (result != 0) {
           calScreen.setText("6");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "6");
        }
    }                                   

    private void btDotActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // button dot 
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("0.");}

        else if (result != 0) {
           calScreen.setText("0.");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + ".");
        }
    }                                     

    private void bt0ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        //  button 0
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("0");}

        else if (result != 0) {
           calScreen.setText("0");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "0");
        }

    }                                   

    private void btEqualActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Result
        secondNumber = Float.parseFloat(calScreen.getText());
        switch(OP){
            case "+" :{
                result = firstNumber +  secondNumber;break;
            }
            case "-":{
                result = firstNumber - secondNumber;break;
            }

            case "*":{
                result = firstNumber * secondNumber;break;
            }
            case "/":{
                result = firstNumber / secondNumber;break;
            }
            default :{
                result = 0 ;break;
            }
        }
        if (result == (int)result){
            calScreen.setText(Integer.toString((int)result));
            anw = Integer.toString((int)result); 
        }//If result is integer
        else {calScreen.setText(Float.toString(result));
              anw = Float.toString(result);
              }// If result is flaot 
        firstNumber= 0;
        secondNumber = 0;
        numberClick = 0;

    }                                       

    private void btPlusActionPerformed(java.awt.event.ActionEvent evt) {                                       
        //Plus button
        firstNumber = Float.parseFloat(calScreen.getText());
        OP = "+";
        numberClick += 1;
        if (!calScreen.getText().equals("+") && numberClick == 2){
            btEqual.doClick();
        }
        else {
            calScreen.setText("+");
        }


    }                                      

    private void btMinusActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // minus button
        firstNumber = Float.parseFloat(calScreen.getText());
        OP = "-";
        numberClick += 1;
        if (!calScreen.getText().equals("-") && numberClick == 2){
            btEqual.doClick();
        }
        else {
            calScreen.setText("-");
        }
    }                                       

    private void btMultiActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Multiply
        firstNumber = Float.parseFloat(calScreen.getText());
        OP = "*";
        numberClick += 1;
        if (!calScreen.getText().equals("x") && numberClick == 2){
            btEqual.doClick();
        }
        else {
            calScreen.setText("x");
        }
    }                                       

    private void btDivideActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // divide button
        firstNumber = Float.parseFloat(calScreen.getText());
        OP = "/";
        numberClick += 1;
        if (!calScreen.getText().equals("/") && numberClick == 2){
            btEqual.doClick();
        }
        else {
            calScreen.setText("/");
        }    
    }                                        

    private void btAnsActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // Answer button
        if (!anw.equals("noInputYet")){
        calScreen.setText(anw);
        }
    }                                     

    private void btSignActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // button Change sign

    }                                      

    private void btPiActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // precent button 

            prNumber = Double.parseDouble(calScreen.getText())/100;
            if (prNumber == (int)prNumber){
            calScreen.setText(Integer.toString((int)prNumber));
            anw = Integer.toString((int)prNumber); 
            result = (int)prNumber; 
              }//prNumber is integer
            else {calScreen.setText(Double.toString(prNumber));
              anw = Double.toString(prNumber);
              result = Float.parseFloat(Double.toString(prNumber));
              }// If prNumber is flaot


    }                                    

    private void ButtonAllClearActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // ALL clear button
        calScreen.setText("0");
        firstNumber = 0;
        secondNumber = 0;
        result = 0;
        numberClick = 0; 
        anw = "noInputYet";
    }                                              

    private void btPwOf2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // button power of 2 
        result = Float.parseFloat(calScreen.getText()) * Float.parseFloat(calScreen.getText());
        if (result == (int)result){
            calScreen.setText(Integer.toString((int)result));
            anw = Integer.toString((int)result); 
              }//prNumber is integer
            else {calScreen.setText(Float.toString(result));
              anw = Float.toString(result);
              }// If prNumber is flaot
    }                                       

    private void calScreenActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // Screen of Calculator

    }                                         

Answer

Take a look at your btMinusActionPerformed method:

private void btMinusActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // minus button
    firstNumber = Float.parseFloat(calScreen.getText());
    OP = "-";
    numberClick += 1;
    if (!calScreen.getText().equals("-") && numberClick == 2){
        btEqual.doClick();
    }
    else {
        calScreen.setText("-"); // <- What does this do?
    }
}

Then take a look at the btEqualActionPerformed method:

private void btEqualActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // Result
    // What is getText() going to return if you pressed the minus button?
    secondNumber = Float.parseFloat(calScreen.getText());
    { ... } // Omitted for brevity
}

Follow that through logically and you’ll notice that when you click the buttons for 1, -, and 2, you will read in 1 and -2. You’re already storing the operand in OP, so what you’ll do is 1 - (-2), which does actually equal 3. Your problem is in btMinusActionPerformed – that’s where you need to fix your code.


Additionally, your operations regular expression isn’t quite correctly defined. You haven’t escaped either the - or the ., so they aren’t going to work in the way that you expect them to.

operations = "[+|\-|x|/|0|\.]",
//               ^         ^
// Note the escape characters at these two locations

That should make your regex work in the way you intended for it to work.

An alternative solution would be to remove:

calScreen.setText("-");

From your btMinusActionPerformed method (and the corresponding lines from the other three operator’s methods), which would mean that the regex wasn’t necessary.

Leave a Reply

Your email address will not be published. Required fields are marked *