Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So i made this code with my friend, but we are having some problems we want to have the "C" button clear the text everytime which it does, unless you have already typed in a problem. Anyway let me know what you think

NOT JAVASCRIPT

/*
 * Antonio and Colin's Amazing Calculator
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Calculator
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
         {
            public void run()
            {
               CalculatorFrame frame = new CalculatorFrame();
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
            }
         });
   }
}
class CalculatorFrame extends JFrame
{
   private static final long serialVersionUID = 1L;

public CalculatorFrame()
   {
      setTitle("Calculator");
      CalculatorPanel panel = new CalculatorPanel();
      add(panel);
      pack();
   }
}
class CalculatorPanel extends JPanel
{
   private static final long serialVersionUID = 1L;
public CalculatorPanel()
   {
      setLayout(new BorderLayout());

      result = 0;
      lastCommand = "=";
      start = true;

      // add the display

      display = new JButton("0");
      display.setEnabled(false);
      display.setBackground(Color.black);
      display.setForeground(Color.white);
      add(display, BorderLayout.NORTH);
      

      ActionListener insert = new InsertAction();
      ActionListener command = new CommandAction();

      // add the buttons in a 4 x 4 grid

      panel = new JPanel();
      panel.setLayout(new GridLayout(5, 2));

      addButton("1", insert);
      addButton("2", insert);
      addButton("3", insert);
      addButton("/", command);

      addButton("4", insert);
      addButton("5", insert);
      addButton("6", insert);
      addButton("*", command);

      addButton("7", insert);
      addButton("8", insert);
      addButton("9", insert);
      addButton("-", command);

      addButton(".", command);
      addButton("0", insert);
      addButton("C", command);
      addButton("+", command);
      
      addButton("", command);
      addButton("^2",command);
      addButton("", command);
      addButton("=", command);

      add(panel, BorderLayout.CENTER);
   }
   private void addButton(String label, ActionListener listener)
   {
      JButton button = new JButton(label);
      button.addActionListener(listener);
      panel.add(button);
   }
   private class InsertAction implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         String input = event.getActionCommand();
         if (start)
         {
            display.setText("");
            start = false;
         }
         display.setText(display.getText() + input);
      }
   }
   private class CommandAction implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         String command = event.getActionCommand();

         if (start)
         {
            if (command.equals("-"))
            {
               display.setText(command);
               start = true;
            }
            else lastCommand = command;
         }
         else
         {
            lastCommand = command;
             calculate(Double.parseDouble(display.getText()));
            
            start = true;
         }
      }
   }
   public void calculate(double x)
   {
      if (lastCommand.equals("C")) result = x * 0.0;
      else
      {
      if (lastCommand.equals("+")) result += x;
      else if (lastCommand.equals("-")) result -= x;
      else if (lastCommand.equals("*")) result *= x;
      else if (lastCommand.equals("/")) result /= x;
      else if (lastCommand.equals("=")) result = x;
      else if (lastCommand.equals("^2")) result = x * x;
      }
     
     
      
      display.setText("" + result);
    }
   private JButton display;
   private JPanel panel;
   private double result;
   private String lastCommand;
   private boolean start;
}

  

share|improve this question

closed as off-topic by Jamal Oct 4 at 3:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

The issue seems to be your start variable. When you type in a number, or more specifically call your InsertAction, you set this start variable to false. Then, when you do an operation (CommandAction), if start is false, then you run that command, and set start to true. If it's true, then you do nothing with the command, just set it to a variable. This is where your issue is.

When you do an operation like 8+9, the outcome of start is false, because the last action was an InsertAction. Because start is false, your CommandAction is run instead of just set, so therefore the screen is cleared. When you do an operation like 8+9=, the last action was a CommandAction, leaving the outcome of start to true. Because of this, when you press C, the command is set to the variable, but never run, so nothing happens.

To fix this issue, you can check if the action is 'C' outside of the start block, and if it is, handle it separately than your other actions. Hope this helps.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.