1

I found this code for a simple Java Swing calculator and ran it in Eclipse, but it's not adding numbers correctly and I'm not sure why.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 A simplified calculator.
 The only operations are addition and subtraction
 */
public class Calculator extends JFrame 
                        implements ActionListener 
{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 200;
    public static final int NUMBER_OF_DIGITS = 30;
    private JTextField ioField;
    private double result = 0.0;

    public static void main (String[] args)
    {
        Calculator aCalculator = new Calculator();
        aCalculator.setVisible(true);
    }

    public Calculator() 
    {
        setTitle("Simplified Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setLayout(new BorderLayout());

        JPanel textPanel = new JPanel();
        textPanel.setLayout(new FlowLayout());
        ioField = 
                new JTextField("Enter numbers here.", NUMBER_OF_DIGITS);
        ioField.setBackground(Color.WHITE);
        textPanel.add(ioField);
        add(textPanel, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground(Color.BLUE);
        buttonPanel.setLayout(new FlowLayout());

        JButton addButton = new JButton("+");
        addButton.addActionListener(this);
        buttonPanel.add(addButton);

        JButton subtractButton = new JButton("-");
        addButton.addActionListener(this);
        buttonPanel.add(subtractButton);

        JButton resetButton = new JButton("Reset");
        resetButton.addActionListener(this);
        buttonPanel.add(resetButton);

        add(buttonPanel, BorderLayout.CENTER);
    }
    public void actionPerformed(ActionEvent e)
    {
        try
        {
            assumingCorrectNumberFormats(e);
        }
        catch (NumberFormatException e2)
        {
            ioField.setText("Error: Reenter Number.");
        }
    }
    //Throws NumberFormatException.
    public void assumingCorrectNumberFormats(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        if (actionCommand.equals("+"))
        {
            result = result + stringToDouble(ioField.getText());
            ioField.setText(Double.toString(result));
        }
        else if (actionCommand.equals("-"))
        {
            result = result - stringToDouble(ioField.getText());
            ioField.setText(Double.toString(result));
        }
        else if (actionCommand.equals("Reset"))
        {
            result = 0.0;
            ioField.setText("0.0");
        }
        else
            ioField.setText("Unexpected error.");
    }
    //Throw NumberFormatException.
    private static double stringToDouble(String stringObject)
    {
        return Double.parseDouble(stringObject.trim());
    }
}

My guess is there's a problem in the assumingCorrectNumberFormats method since that's what actually handles the event listeners in actionPerformed, but I can't see where there's a flaw in there.

What actually happens when I run the program: If I enter 10 then plus, the text box immediately outputs 20, as if I had already entered 10 implicitly. If I repeatedly press plus, the number in the text box then gets multiplied by 4 each time.

ericgrosse
  • 1,490
  • 20
  • 37

2 Answers2

3

I find an error in your program. Please examine these code:

 JButton subtractButton = new JButton("-");
        addButton.addActionListener(this);
        buttonPanel.add(subtractButton);

In the second line,the addButton should be subtractButton.

Eugene
  • 10,627
  • 5
  • 49
  • 67
3

It's not your code fault, system getting keyboard command twice.

    if (actionCommand.equals("+"))
    {
        System.out.println(ioField.getText());
        result = result + stringToDouble(ioField.getText());
        ioField.setText(Double.toString(result));
    }

Try this with your code, you'll get that, what's happening exactly.

Ysr Shk
  • 224
  • 5
  • 16