This is a working program.
But there is a logical error.
Try this with the calculator:
Press 2,M;
Press R,*,2;
Press R,*2;
The output in the same order must be:
2
2*2 = 4
2*2 = 4
but the third one outputs 88 instead.

M stands for Memory, it stores the value and R calls that value to be used in future operations. Why is this error so?

package MyJava; //change package name according to what 
//package you installed the class in

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

public class De_Galicia6 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 char op;
    private boolean firstInput = true;
    private String memory = null;

    public De_Galicia6 () {
        setTitle("Calculator");
        setSize(230, 230);
        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() {
            @Override
        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 'R':
            displayText.setText(memory);
            if (firstInput) 
                numStr1 = memory;
            else 
                numStr2 = memory;

            break;
        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;

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

        case 'M':
            memory = displayText.getText();
            break;
        case 'E':
            System.exit(0);
    }
}

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

    try {
        float num1 = Float.parseFloat(numStr1);
        float num2 = Float.parseFloat(numStr2);
        float result = (float) 0.00;

        switch (op) {
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
                break;
        }

        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) {
        De_Galicia6 C = new De_Galicia6();
    }
}

for sequence 2MR*2=R*2= ok.

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

but for 2MR*2R*2 no ok. [22]

I think you have a little design flaw. The result is not always the first input, and I think that is what is causing the problem. I am not completly sure though. Try to rethink your design with a piece of paper and a pen.

Well, turned out to be that the program isn't really designed to comply to my sample run. :P

Well, thanks anyways guys, Appreciated the help. :D

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.