title: How to account for input errors in calculator program?
body: <p>I'm writing a calculator program for my <code>java class</code>. I want to account for errors like</p>
  
  <ol>
  <li><p>User decides to divide by 0. I want the calculator to be able to acknowledge this error and display ERROR.</p></li>
  <li><p>The user enters 6+7+ and then hits the equal button. </p></li>
  </ol>
  
  <p>I want, once again, for the calculator to acknowledge that there is an error. I was hoping I could get some hints as to how to go about this. Thanks!</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import java.awt.event.*;
  public class Calculator
  {
  JFrame window;
  // stuff for top panel
  JPanel topPanel;
  JTextField expr,result;
  JButton equals;
  
  // stuff for bottom panel
  
  JPanel bottomPanel,digitsPanel,opsPanel;
  JButton[] digits,ops;
  JButton clear, clearEntry;
  Container content;
  Listener listener;
  String[] oplabels = { "+", "-", "/", "*" };
  
  public Calculator()
  {
      listener = new Listener(); // our Listener class implements ActionListener
      window= new JFrame("GUI Calc");
      content=window.getContentPane();
      content.setLayout( new GridLayout(2,1) );
      topPanel=new JPanel();
      topPanel.setLayout( new GridLayout(1,3) );
  
      // TOP PANEL WORK
  
      expr = new JTextField( );
      equals = new JButton("=");
      equals.addActionListener( listener );
      result = new JTextField( );
  
      topPanel.add( expr );
      topPanel.add( equals );
      topPanel.add( result );
  
      // BOTTOM PANEL WORK
  
      bottomPanel = new JPanel();
      bottomPanel.setLayout( new GridLayout(1,2) );
  
      digitsPanel = new JPanel();
      digitsPanel.setLayout( new GridLayout(4,3) );
  
      opsPanel = new JPanel();
      opsPanel.setLayout( new GridLayout(4,1) );
  
      digits  = new JButton[12];
      ops = new JButton[4];
  
      for (int i=0 ; i&lt;10 ; i++)
      {
          digits[i] = new JButton( i+"" );
          digits[i].addActionListener(listener);
          digitsPanel.add( digits[i] );
      }
  
      clear = new JButton( "C" );
      clearEntry = new JButton( "CE" );
      clear.addActionListener(listener);
      clearEntry.addActionListener(listener);
      digitsPanel.add( clear );
      digitsPanel.add( clearEntry);
  
  
      for (int i=0 ; i&lt;4 ; i++)
      {
          ops[i] = new JButton( oplabels[i] ) ;
          ops[i].addActionListener(listener);
          opsPanel.add( ops[i] );
      }
  
      bottomPanel.add( digitsPanel );
      bottomPanel.add( opsPanel );
  
  
      content.add( topPanel);
      content.add( bottomPanel);
  
      window.setVisible(true);
  }
  class Listener implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
      {
          Component clicked = (Component) e.getSource();
          if ( clicked == equals )
          {
              result.setText( evaluate( expr.getText() ) );
              return;
          }
          for ( int i=0 ; i&lt;10 ; i++)
          {
              if ( clicked == digits[i] )
              {
                  expr.setText( expr.getText() + i );
                  return;
              }
          }
          if ( clicked == clear )
          {
              expr.setText("0");
              return;
              // do something
  
          }
  /*          if (clicked == clearEntry)
          {
          expr.setText(expr.getDigits[i]);
          }
          for (int i=0; i &lt; 10; i++)
          {
              if (clicked == digits[i])
              {
                  int lastValue = clicked;
                  if(clicked == clearEntry)
                  {
                  expr.setText(expr.getText()-clicked);
                  return;
                  }
              }
          } */
  
          for ( int i=0 ; i&lt;4 ; i++ )
          {
              if(clicked == digits[i])
              {
              expr.setText(expr.getText() + oplabels[i]);
              return;
              }
              // tack on that operator to the expr string
          }
  
      }
      String evaluate( String exp )
      {
          return "NOT WRITTEN YET";
      }
  }
  public static void main(String [] args)
  {
      new Calculator();
  }
  }
  </code></pre>
  
  <p>I'm still working on it. Especially where I'm trying to figure out the clearEntry button so I have put that section as a comment because its still a work in progress. </p>