Angel
Angel

Reputation: 3

Calculator GUI problems

I'm in my first semester of programming as a first year college and we were tasked to make a calculator GUI. I'm almost done but I need to make the "Error" appear if the denominator is 0 but it outputs 0.0. My other problem is that I need the gui to restart after showing the final answer but what happens is that after I clicked equals then clicked the number it just continues. So if I press 1+1 then clicked =, it outputs 2 but when I clicked a number for example 1, it just becomes 21.

Also, how do I remove the .0 at the end of every answer? I tried endsWith and replace after every equation but it's not working.

 @Override
public void actionPerformed(ActionEvent e){
    for(int i=0;i<10;i++) {
        if(e.getSource() == numbers[i]) {
            text.setText(text.getText().concat(String.valueOf(i)));
        }
    }
    if(e.getSource()==dec) {
        if (text.getText().contains(".")) {
            return;
        } else {
            text.setText(text.getText() + ".");
        }
    }
    if(e.getSource()==add) {
        num1 = Double.parseDouble(text.getText());
        operator ='+';
        label.setText(text.getText() + "+");
        text.setText("");
    }
    if(e.getSource()==sub) {
        num1 = Double.parseDouble(text.getText());
        operator ='-';
        label.setText(text.getText() + "-");
        text.setText("");
    }
    if(e.getSource()==mult) {
        num1 = Double.parseDouble(text.getText());
        operator ='*';
        label.setText(text.getText() + "*");
        text.setText("");
    }
    if(e.getSource()==div) {
        num1 = Double.parseDouble(text.getText());
        operator ='/';
        label.setText(text.getText() + "/");
        text.setText("");
    }
    if(e.getSource()==neg) {
        Double neg = Double.parseDouble(text.getText());
        neg*=-1;
        text.setText(String.valueOf(neg));
    }
    if(e.getSource()==per) {
        num1 = Double.parseDouble(text.getText())/100;
        label.setText(text.getText() + "%");
        text.setText(String.valueOf(num1));
        label.setText("");
    }
    
    
    if(e.getSource()==equ) {
        num2=Double.parseDouble(text.getText());
        switch(operator) {
        case'+':
            ans=num1+num2;
            break;
        case'-':
            ans=num1-num2;
            break;
        case'*':
            ans=num1*num2;
            break;
        case'/':
            if (num2==0)
                text.setText("Error");
                else
                    ans=num1/num2;
            break;
        }
        label.setText("");
        text.setText(String.valueOf(ans));
    }
    }
    }   
    }

Upvotes: 0

Views: 358

Answers (1)

Alex Sveshnikov
Alex Sveshnikov

Reputation: 4329

  • Regarding your first question about displaying "Error" when denominator is zero.
    When you press "=" button you first get into '/' branch of the switch(operator) statement. There you check that num2 is equal to zero and set the text field to "Error". However, after that you exit the switch statement and continue with label.setText(""); followed by text.setText(String.valueOf(ans));. Because the current value of ans is zero, the last statement sets the value of text field to "0.0" overwriting the previous value of "Error". There are different ways to deal with that. Now when you understand the cause of the problem you can try to find the solution yourself.

  • Regarding your second question about how to reset the state of the calculator. For example you can create a boolean state variable, which will be true if the calculator is ready for new input and false if it is in the middle of some input. You initialize this state variable to true and then every time you press some button you set it to false. If the state variable is true, then reset the text field to an empty string before you append a number to it. Finally, set the state variable back to true after the user has pressed '=' button. So, you can do something like this:

// We reset the text field to an empty string at the start of each calculation
if (state) { 
  text.setText("");
}

// Here is your code

.....

// And at the very end:
// Change the state to false if user pressed anything except the equality sign
state = e.getSource()==equ;

Upvotes: 1

Related Questions