2

I am currently working on a GUI calculator in NetBeans. I have my addition operator working well, but as for my subtraction, multiplication, and division it returns wrong values. I don't think I have problem in my logic with the calculation.

Value that the operator returns:

Subtraction: sum of the input values with negative sign

Multiplication 0

Division 0

Here are my buttons and functions for the operators:

// add button
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
    operator = "+";
    calculation(operator);
} 

// subtract button
private void btnSubtractActionPerformed(java.awt.event.ActionEvent evt) {                                            
    operator = "-";
    calculation(operator);
} 

// multiply button
private void btnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {                                            
    operator = "*";
    calculation(operator);
}

// divide button
private void btnDivideActionPerformed(java.awt.event.ActionEvent evt) {                                          
    operator = "/";
    calculation(operator);
} 

// equals button
private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                          
    calculation(operator);
    txtInput.setText(String.valueOf(accum2));
}


// calculation method
public void calculation(String operator){
    if(operator.equals("+")) {
        accum2 += accum1;
        accum1 = 0;
    } else if(operator.equals("-")){
        accum2 -= accum1;
        accum1 = 0;
    } else if(operator.equals("*")){
        accum2 *= accum1;
        accum1 = 0;
    } else if(operator.equals("/")){
        accum2 /= accum1;
        accum1 = 0;
    } else {
        accum2 = accum1;
        accum1 = 0;
    }
}
Ricky Aguilar
  • 329
  • 1
  • 8
  • 19
  • What type are `accum1` and `accum2`? – Henrik Aasted Sørensen Oct 20 '17 at 06:29
  • In the equals you better set operator to "=" and add the check in the calculate method otherwise it has the value of the previous operation. I think that messes up your * and / because it actually divides or multiplies by zero. Do you have the problem when you press equals? Or when you keep multiplying – Veselin Davidov Oct 20 '17 at 06:30
  • @Henrik accum1 and accum2 are int. – Ricky Aguilar Oct 20 '17 at 06:32
  • @VeselinDavidov I don't think it is dividing or multiplying to zero. I test the multiply button by using addition first to put value in my accum1 and accum2 but when I click multiply then equals, it still returns 0. The problem is in the first equals except for addition. – Ricky Aguilar Oct 20 '17 at 06:37
  • Yes the problem in equals is (or at least one of the problems) that you have the previous operator. So for example you do multiply and your operator is "*" and acum1 is 0 after calculate method. Then you press equals and in the equals method you go in calculation - the operator is still * since noone changed it but acum1 is 0 so the result becomes 0. Try giving the equals a value = and see what will happen ;) – Veselin Davidov Oct 20 '17 at 06:40
  • @VeselinDavidov seems like I am calculating them with 0. I will try to check the values of accum1 and accum2 while undergoing calculation method. Thank you. – Ricky Aguilar Oct 20 '17 at 06:53

1 Answers1

0

I think the problems are the values of accum2 and accum2 before calculating the result. So can you check their values by using System.out.println(" accum 1 "+ accum1) and System.out.println(" accum 2 "+ accum2) before performing the computation.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tadesse
  • 76
  • 2
  • 4