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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.