I'm trying to add a memory function, that when you hit the 'M' button, it stores whats in the display into a variable, and when you hit the 'R' button, it retrieves the variable value and displays it and can be used for calculations. Here's my code:

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

public class Calculator extends JFrame implements ActionListener
{
    private JTextField displayText = new JTextField(30);
  
    private JButton[] button = new JButton[19];
    
	 private String[] keys = {"7", "8", "9", "/",
                                               "4", "5", "6", "*",
                                               "1", "2", "3", "-",
                                               "0", "C", "=", "+",
                                               "M", "R", "E"};

    private String numStr1 = "";
    private String numStr2 = "";
    private String memory = "";
    private char op;
    private boolean firstInput = true;

    public Calculator()
    {       
        setTitle("My Calculator");
        setSize(235, 240);
        Container pane = getContentPane();

        pane.setLayout(null);

        displayText.setSize(200, 30);
        displayText.setLocation(10, 10);
        pane.add(displayText);

        int x, y;

        x = 10;
        y = 40;

        for (int ind = 0; ind < 19; ind++)
        {
            button[ind] = new JButton(keys[ind]);
            button[ind].addActionListener(this);
            button[ind].setSize(50, 30);
            button[ind].setLocation(x, y);
            pane.add(button[ind]);
            x = x + 50;

            if ((ind + 1) % 4 == 0)
            {
                x = 10;
                y = y + 30;
            }
        }

        this.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            }
        );

        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        String resultStr;                          
 
	String str =
            String.valueOf(e.getActionCommand());  

        char ch = str.charAt(0);                  

        switch (ch)                                
        {
        case '0':                                  
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            if (firstInput)
            {
                numStr1 = numStr1 + ch;
                displayText.setText(numStr1);
            }
            else
            {
                numStr2 = numStr2 + ch;
                displayText.setText(numStr2);
            }
            break;

        case '+':                       
        case '-':
        case '*':
        case '/':
            op = ch;
            firstInput = false;
            break;

        case '=':                  
            resultStr = evaluate();
            displayText.setText(resultStr);
            numStr1 = resultStr;
            numStr2 = "";
            firstInput = false;
            break;

 //my memory button function	  
           case 'M':
		  displayText.setText(memory);				
		  break; 
  
 //my retreive memory button function
           case 'R':
		  return Str.valueOf(memory);
		  numStr1 = memory;
		  break;
				
          case 'E':
		  System.exit(0);
		break;

        case 'C':                   
            displayText.setText("");
            numStr1 = "";
            numStr2 = "";
            firstInput = true;
        
		  		
		  }
    }

    private String evaluate()
    {
        final char beep = '\u0007';

        try
        {
            int num1 = Integer.parseInt(numStr1);
            int num2 = Integer.parseInt(numStr2);
            int result = 0;

            switch (op)
            {
            case '+':
                result = num1 + num2;
                break;

            case '-':
                result = num1 - num2;
                break;

            case '*':
                result = num1 * num2;
                break;

            case '/':
                result = num1 / num2;
            }

            return String.valueOf(result);
        }
        catch (ArithmeticException e)
        {
            System.out.print(beep);
            return "E R R O R: " + e.getMessage();
        }
        catch (NumberFormatException e)
        {
            System.out.print(beep);
            if (numStr1.equals(""))
               return "E R R O R: Invalid First Number" ;
            else
               return "E R R O R: Invalid Second Number" ;
        }
        catch (Exception e)
        {
            System.out.print(beep);
            return "E R R O R";
        }
    }

    public static void main(String[] args)
    {
        Calculator C = new Calculator();
    }
}

I've tried a few different variations, and I think it may be something very simple that I'm doing wrong.

What problem are you having and what is your question?

//my memory button function	  
		  case 'M':
		  		displayText.setText(memory);				
				break; 
  
 //my retreive memory button function
        case 'R':
		      return Str.valueOf(memory);
		      numStr1 = memory;
				break;

This is the part I'm having trouble with. I can't get what's in the displayfield to store into the 'memory' variable and have it displayed back to me.

As far as I can tell, it looks like you're trying to return a value from a method that isn't supposed to return anything.

return Str.valueOf(memory);
public void actionPerformed(ActionEvent e)

I'm pretty new to Java, so that could be wrong.

Thanks for the input guys. I kinda got it to work. I needed to switch some things around. Here's what I did:

//my memory button function	  
      case 'M':
		memory = numStr1;				
		break; 
  
 //my retreive memory button function
       case 'R':
		displayText.setText(memory);				
		numStr1 = memory;
		break;

What I really need to know is how can I store what's showing in the displayTextfield into memory, instead of the work-around I did?

Ok, figured it out finally. Here's what I had to do:

//my memory button function	  
		  case 'M':
		  	memory = displayText.getText();				
			break;

Hope this thread might help someone in the future!

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.