0

When I add or minus or try to sum up any operation it dose not work. This is what I have and I believe it has something to do with the code that shows in the box

//////////
/////////

What am i doing wrong? The parent class has the buttons as an array.

public void actionPerformed(ActionEvent e) {

    // Get the source of this action
    JButton clickedButton = (JButton) e.getSource();

    // Get the existing text from the calculator
    // displayField.
    String dispFieldText = parent.getDisplayValue();
    dot = dispFieldText.contains(".");
    if (!dispFieldText.equals(""))
        enteredNumber = Double.parseDouble(dispFieldText);

    if (clickedButton.getText().equals("+")) {
        selectedAction = "+";
        currentNumber = enteredNumber;
        parent.setDisplayValue("");

    } else if (clickedButton.getText().equals("-")) {
        selectedAction = "-";
        currentNumber = enteredNumber;
        parent.setDisplayValue("");

    } else if (clickedButton.getText().equals("*")) {
        selectedAction = "*";
        currentNumber = enteredNumber;
        parent.setDisplayValue("");

    } else if (clickedButton.getText().equals("/")) {
        selectedAction = "/";
        currentNumber = enteredNumber;
        parent.setDisplayValue("");

    ////////////////////////////////////////////////////////////////////////////////
    } else if ((clickedButton == parent.buttons[11]) && (selectedAction != null)) {
        if (selectedAction.equals("+")) {
            currentNumber += enteredNumber;
            parent.setDisplayValue("" + currentNumber);
    ////////////////////////////////////////////////////////////////////////////////        

        } else if (selectedAction.equals("-")) {
            currentNumber -= enteredNumber;
            parent.setDisplayValue("" + currentNumber);

        } else if (selectedAction.equals("*")) {
            currentNumber *= enteredNumber;
            parent.setDisplayValue("" + currentNumber);

        } else if (selectedAction.equals("/")) {
            currentNumber /= enteredNumber;
            parent.setDisplayValue("" + currentNumber);

        } 

    }
    else {
            if (!clickedButton.getText().equals("=")) {
                if ((clickedButton.getText().equals(".")) && dot) {
                }

                else {
                    // Get the button label
                    String clickedButtonLabel = clickedButton.getText();
                    parent.setDisplayValue(dispFieldText + clickedButtonLabel);
                }
            }

        }
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Meant to add that this is for a calculator. – user3055624 Mar 17 '18 at 00:32
  • 1
    For better help sooner post a proper [mcve], here's a sample of a [calculator](https://stackoverflow.com/questions/39234273/how-do-i-resize-jcomponents-in-gridlayout/39235219#39235219) also this [probably related](https://stackoverflow.com/questions/34164022/calculator-returns-0-0-to-all-questions-asked/34164167#34164167) – Frakcool Mar 17 '18 at 00:40
  • Hi. thanks for the help. I have the GUI setup buts it's the functionality that's causing a problem. When I hit the equal button ( = ) it doesn't add the two numbers that I inputted. – user3055624 Mar 17 '18 at 01:33
  • `I believe it has something to do with the code that shows in the box` - so did you debug that code? Did you add System.out.println(...) statements to see what the value of the variables are? Does the code even get executed. Do some basic debugging and don't guess where the problem is. Tell us what the problem is and maybe we can then help. – camickr Mar 17 '18 at 01:53
  • Ok I will try that. Thanks. – user3055624 Mar 17 '18 at 03:27
  • See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Mar 17 '18 at 03:38
  • 1
    *"Ok I will try that."* Who are you replying to? Tip: Add @Frakcool (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Mar 18 '18 at 23:00

0 Answers0