0

Hello,

I have a calculator that has a set of memory buttons that I want to store the result of a calculation in..

Here is the code:

ActionListener memPadListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String memOperator = e.getActionCommand();
				double numForMem = 0.0;

				if (memOperator.equals("M+")) {
					numForMem = Double.valueOf(calDisplay.getText());
				
	actionClear();
				} else if (memOperator.equals("M-")) {

				} else if (memOperator.equals("MR")) {
					String numForMemToDisplay = Double.toString(numForMem);
				
	calDisplay.setText(numForMemToDisplay);
				} else if (memOperator.equals("MC")) {
					numForMem = 0.0;
					actionClear();
				}
			}

		};
		JPanel memPadPanel = new JPanel(new GridLayout(4, 1, 1, 1));
		String[] memPad = { "MR", "MC", "M+", "M-" };
		for (int i = 0; i < memPad.length; i++) {
			JButton memButton = new JButton(memPad[i]);
			memButton.addActionListener(memPadListener);
			memPadPanel.add(memButton);
		}
		insideCalPanel.add(memPadPanel, BorderLayout.WEST);
	}

But when I try and retrieve the value that is stored in memory, it returns just 0

Any suggestions please?

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by quuba
0

Line 6.
numForMem should be declared outside an actionPerformed method.
Now numForMem is a local.

Edited by quuba: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.