I am pretty fresh in the java environment but i am working hard to learn. I wrote the code for a calculator, however I am having problems in making the memory buttons function properly. I have to create 4 memory buttons that function in the following way: When the memory button is pushed once, whatever value is in the textfield has to be remembered. When the same memory button is pushed again, the remembered value has to be placed into the text field, and so on. The major conflict is that all four buttons must store and draw their values from the same array. If you can please help me with this, give any suggestions !! Your help is very much appreciated.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
public static final int WIDTH = 225;
public static final int HEIGHT = 225;
public static final int ROWS = 5;
public static final int COLUMNS = 4;
private JTextField display;
private boolean transform = false;
private String equal = "=";
private double argum = 0;

// This constructor creates the GUI.
public Calculator()
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
display = new JTextField("0");
cp.add(display, "North");
JPanel bp = new JPanel();
bp.setLayout(new GridLayout (ROWS,COLUMNS));
String buttons = "789/456*123-0.=+";
for (int n = 0; n < buttons.length(); n++)
addButtons(bp, buttons.substring(n, n + 1));
JButton memo1 = new JButton("M1");
JButton memo2 = new JButton("M2");
JButton memo3 = new JButton("M3");
JButton memo4 = new JButton("M4");
cp.add(bp, "Center");
private void addButtons(Container con, String str)
JButton but = new JButton(str);
public void actionPerformed(ActionEvent e)
String str = e.getActionCommand();
double[] memo = new double[4];

if ('0' <= str.charAt(0) && str.charAt(0) <= '9'
|| str.equals("."))
if (transform) display.setText(str);
else display.setText(display.getText() + str);
transform = false;
double val = (Double.valueOf(display.getText())).doubleValue();
equal = str;
//These memory buttons functions are not working !!
if (str.equals("M1")) memo[0] = (Double.valueOf(display.getText())).doubleValue();
{if (equal.equals("M1")) display.setText(""+memo[0]);}

transform = true;

public void calculate(double num)

if (equal.equals("+")) argum += num;
else if (equal.equals("-")) argum -= num;
else if (equal.equals("*")) argum *= num;
else if (equal.equals("/")) argum /= num;
else if (equal.equals("=")) argum = num;

display.setText("" + argum);
// This is the main of the class Calculator.
public static void main(String[] args)
JFrame f = new Calculator();
} // end of main.
} // end of class Calculator.

One reason why your memory buttons don't work: The array you're using to store the values is declared in the actionPerformed method. This means that as soon as the method returns, the array goes away forever. If you move the declaration somewhere with a larger scope, e.g. make it a member of your Calculator class, the stored values will stick around after the action processing has finished.

This article has been dead for over six months. Start a new discussion instead.