title: Java Calculator - getting numbers to stay on screen before an operation is pressed
body: <p>I'm having trouble with my calculator; everything runs fine. but when an operator is entered, I need the number just previously entered to stay on the screen, as well as the operator.
  I also need a way to fix errors that occur when equals is pressed after just an operator is entered.<br>
  Any help would be appreciated. Thanks</p>
  
  <pre><code>import java.awt.*; 
  import javax.swing.*; 
  import java.awt.event.ActionListener;
  import java.awt.event.ActionEvent;
  import java.awt.event.*;
  import javax.swing.JLabel;
  
  
  public class Chelculator extends JPanel{
  
    //creates buttons
    private JTextField screen; //creates the screen
    private JButton delete, clear; //creates clear and delete buttons
    private JButton zero, one, two, three, four, five, six, seven, eight, nine; //creates number buttons
    private JButton plus, minus, square, divide, multiply, squareroot, point, equals; //creates operation buttons
    private JButton mclear, mplus, mminus, mrecall; //creates memory buttons
    double Mem; 
    double Subtract;
  
    double number1, number2, result;
    char choice;
    static String lastCommand = null;
  
    public Chelculator(){
      JPanel buttons = new JPanel();
      buttons.setBackground(Color.pink); //changes background to pink
      plus = new JButton("+"); 
      minus = new JButton("-");
      square = new JButton("^");
      clear = new JButton("CE");
      divide = new JButton("÷");
      multiply = new JButton("x");
      squareroot = new JButton("√");
      delete = new JButton("DE");
      mclear = new JButton("MC");
      mplus = new JButton("M+");
      mminus = new JButton("M-");
      mrecall = new JButton("MR");
      one = new JButton("1");
      two = new JButton("2");
      three = new JButton("3");
      four = new JButton("4");
      five = new JButton("5");
      six = new JButton("6");
      seven = new JButton("7");
      eight = new JButton("8");
      nine = new JButton("9");
      zero = new JButton("0");
      equals = new JButton("=");
      point = new JButton(".");
  
  
      buttons.setLayout(new GridLayout(6,4)); //creates a grid for buttons
  
      //adds buttons into calculator
      buttons.add(mclear);
      buttons.add(mrecall);
      buttons.add(mplus);
      buttons.add(mminus);
      buttons.add(clear);
      clear.setForeground(Color.red);
      buttons.add(delete);
      buttons.add(square);
      buttons.add(squareroot);
      buttons.add(plus);
      buttons.add(minus);
      buttons.add(divide);
      buttons.add(multiply);
      buttons.add(seven);
      buttons.add(eight);
      buttons.add(nine);
      buttons.add(zero);
      buttons.add(four);
      buttons.add(five);
      buttons.add(six);
      buttons.add(point);
      buttons.add(one);
      buttons.add(two);
      buttons.add(three);
      buttons.add(equals);
  
      EventListener Listener = new EventListener();
      plus.addActionListener(Listener); 
      minus.addActionListener(Listener); 
      square.addActionListener(Listener); 
      clear.addActionListener(Listener); 
      divide.addActionListener(Listener); 
      multiply.addActionListener(Listener); 
      squareroot.addActionListener(Listener); 
      delete.addActionListener(Listener); 
      mclear.addActionListener(Listener); 
      mplus.addActionListener(Listener); 
      mminus.addActionListener(Listener); 
      mrecall.addActionListener(Listener); 
      one.addActionListener(Listener);
      two.addActionListener(Listener);
      three.addActionListener(Listener);
      four.addActionListener(Listener);
      five.addActionListener(Listener);
      six.addActionListener(Listener); 
      seven.addActionListener(Listener);
      eight.addActionListener(Listener); 
      nine.addActionListener(Listener); 
      zero.addActionListener(Listener); 
      equals.addActionListener(Listener); 
      point.addActionListener(Listener);  
  
      setPreferredSize(new Dimension(270,280));
      screen = new JTextField();
      screen.setEditable(false);
      JPanel screens = new JPanel();
      screens.setLayout(new BorderLayout());
      screens.setBackground(Color.pink);
      screens.setPreferredSize(new Dimension(270,40)); //creates a panel and puts screen in it. This allows the size of the screen to be changed.
      screens.add(screen);
      setLayout(new BorderLayout()); 
      add(screens, BorderLayout.NORTH); 
      add(buttons, BorderLayout.CENTER); 
    }
  
    private class EventListener implements ActionListener{ 
  
      public void actionPerformed(ActionEvent e){
    String input = e.getActionCommand();
        if(Character.isDigit(input.charAt(0))){ 
          screen.setText(screen.getText() + input); 
        }    
  
        if(e.getSource() == equals){
          equal();
        }
  
  
        if(e.getSource() == plus){
          number1 = Double.parseDouble(screen.getText());
          screen.setText("");
          choice = '+';
        }  
        if(e.getSource() == minus){
          number1 = Double.parseDouble(screen.getText());
          screen.setText("");
          choice = '-';
        }
        if(e.getSource() == square){
          screen.setText("^");
        }
        if(e.getSource() == squareroot){
          screen.setText("√");
        }
        if(e.getSource() == divide){
          number1 = Double.parseDouble(screen.getText());
          screen.setText("");
          choice = '/';
        }
  
        if(e.getSource() == multiply){
          number1 = Double.parseDouble(screen.getText());
          screen.setText("");
          choice = 'x';
        }
  
        if(e.getSource() == clear){
          screen.setText(null);
          number1 = 0;
          number2 = 0;
        }
  
        if(e.getSource() == delete){ 
         screen.getText().substring(0, (screen.getText().length() - 1));  
        }
  
        if(e.getSource() == point){
          screen.setText(screen.getText() + "."); 
        }
  
  
        //MEMORY
        if(e.getSource() == mplus){
          Mem = Mem + Double.parseDouble(screen.getText());
      }
  
        if(e.getSource() == mrecall){
          screen.setText(Double.toString(Mem));
        }
  
        if(e.getSource() == mminus){
          Subtract = Double.parseDouble(screen.getText());
          Mem = Mem - Subtract;
    }
  
      if(e.getSource() == mclear){
        Mem = 0;
      }
      }
  
    public void equal(){
      number2 = Double.parseDouble(screen.getText());
      if (choice == '+'){
        result = number1 + number2;
        number1 = 0;
        number2 = 0;
  
      }
      if (choice == '-'){
        result = number1 - number2;
        number1 = 0;
        number2 = 0;
      }
      if (choice == '/'){
        result = number1 / number2;
        number1 = 0;
        number2 = 0;
      }
      if (choice == '*'){
        result = number1 * number2;
        number1 = 0;
        number2 = 0;
      }
  
      screen.setText(Double.toString(result));
    }
  
    private void assign(String no){
      if(screen.getText().equals("0"))
        screen.setText(no);
      else if(lastCommand == "="){
        screen.setText(no);
        lastCommand = null;
      }
      else
        screen.setText(screen.getText()+no);
    }
  }
  }
  </code></pre>