-1

I'm fully aware there are better ways to make this but id be perfectly happy with it as long as it finally worked, whenever i do an operation like 4+4 its correct but if i do something like 4+4*2 it will equal 32

package calc;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
*
* @author Ben
*/
public class GUI extends JFrame {

    int response, operator;
    double num1, num2, total = 0;
    String operation, answer, num, testnum;
    private JButton one, two, three, four, five, six, seven, eight,
    nine, zero, multiply, divide, subtract, add, equals, clear;
    private JTextField display, fakedisplay;

    public GUI() {
        super("Calculator");
        setLayout(new FlowLayout());

        fakedisplay = new JTextField(10);
        display = new JTextField(10);;
        add(display);
        one = new JButton("1");
        add(one);
        two = new JButton("2");
        add(two);
        three = new JButton("3");
        add(three);
        four = new JButton("4");
        add(four);
        five = new JButton("5");
        add(five);
        six = new JButton("6");
        add(six);
        seven = new JButton("7");
        add(seven);
        eight = new JButton("8");
        add(eight);
        nine = new JButton("9");
        add(nine);
        zero = new JButton("0");
        add(zero);
        multiply = new JButton("*");
        add(multiply);
        divide = new JButton("/");
        add(divide);
        subtract = new JButton("-");
        add(subtract);
        add = new JButton("+");
        add(add);
        equals = new JButton("=");
        add(equals);
        clear = new JButton("Clear");
        add(clear);


        handler handle = new handler();

        one.addActionListener(handle);
        two.addActionListener(handle);
        three.addActionListener(handle);
        four.addActionListener(handle);
        five.addActionListener(handle);
        six.addActionListener(handle);
        seven.addActionListener(handle);
        eight.addActionListener(handle);
        nine.addActionListener(handle);
        zero.addActionListener(handle);
        multiply.addActionListener(handle);
        divide.addActionListener(handle);
        subtract.addActionListener(handle);
        add.addActionListener(handle);
        equals.addActionListener(handle);
        clear.addActionListener(handle);

    }
    private class handler implements ActionListener {

        @
        Override
        public void actionPerformed(ActionEvent e) {

            if(e.getSource() == one) {
                response = 1;
                display.setText(display.getText() + 1);
                fakedisplay.setText(fakedisplay.getText() + 1);
            } else if(e.getSource() == two) {
                response = 2;
                display.setText(display.getText() + 2);
                fakedisplay.setText(fakedisplay.getText() + 2);
            } else if(e.getSource() == three) {
                response = 3;
                display.setText(display.getText() + 3);
                fakedisplay.setText(fakedisplay.getText() + 3);
            } else if(e.getSource() == four) {
                response = 4;
                display.setText(display.getText() + 4);
                fakedisplay.setText(fakedisplay.getText() + 4);
            } else if(e.getSource() == five) {
                response = 5;
                display.setText(display.getText() + 5);
                fakedisplay.setText(fakedisplay.getText() + 5);
            } else if(e.getSource() == six) {
                response = 6;
                display.setText(display.getText() + 6);
                fakedisplay.setText(fakedisplay.getText() + 6);
            } else if(e.getSource() == seven) {
                response = 7;
                display.setText(display.getText() + 7);
                fakedisplay.setText(fakedisplay.getText() + 7);
            } else if(e.getSource() == eight) {
                response = 8;
                display.setText(display.getText() + 8);
                fakedisplay.setText(fakedisplay.getText() + 8);
            } else if(e.getSource() == nine) {
                response = 9;
                display.setText(display.getText() + 9);
                fakedisplay.setText(fakedisplay.getText() + 9);
            } else if(e.getSource() == zero) {
                response = 0;
                display.setText(display.getText() + 0);
                fakedisplay.setText(fakedisplay.getText() + 0);
            } else if(e.getSource() == multiply) {
                if(operator == 0 && num1 == 0) {
                    num1 = Double.parseDouble(display.getText());
                    operator = 3;
                    fakedisplay.setText("");
                } else if(operator != 0) {
                    operator = 3;
                    num2 = Double.parseDouble(fakedisplay.getText());
                    fakedisplay.setText("");
                    if(operator == 1) {
                        total = num1 + num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 2) {
                        total = num1 - num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 3) {
                        total = num1 * num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 4) {
                        total = num1 / num2 + num1 % num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    num2 = 0;
                    total = 0;
                    fakedisplay.setText("");
                    operator = 0;
                }
                operation = "*";
                display.setText(display.getText() + operation);

            } else if(e.getSource() == divide) {
                if(operator == 0 && num1 == 0) {
                    num1 = Double.parseDouble(display.getText());
                    operator = 4;
                    fakedisplay.setText("");
                } else if(total == 0) {
                    operator = 3;
                } else if(operator != 0) {
                    operator = 4;
                    num2 = Double.parseDouble(fakedisplay.getText());
                    fakedisplay.setText("");
                    if(operator == 1) {
                        total = num1 + num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 2) {
                        total = num1 - num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 3) {
                        total = num1 * num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 4) {
                        total = num1 / num2 + num1 % num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    num2 = 0;
                    total = 0;
                    fakedisplay.setText("");
                    operator = 0;

                }
                operation = "/";
                display.setText(display.getText() + operation);

            } else if(e.getSource() == add) {
                if(operator == 0 && num1 == 0) {
                    num1 = Double.parseDouble(display.getText());
                    operator = 1;
                    fakedisplay.setText("");
                } else if(operator != 0) {
                    operator = 1;
                    num2 = Double.parseDouble(fakedisplay.getText());
                    fakedisplay.setText("");
                    if(operator == 1) {
                        total = num1 + num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 2) {
                        total = num1 - num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 3) {
                        total = num1 * num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 4) {
                        total = num1 / num2 + num1 % num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    num2 = 0;
                    total = 0;
                    fakedisplay.setText("");
                    operator = 0;
                }
                operation = "+";
                display.setText(display.getText() + operation);

            } else if(e.getSource() == subtract) {

                if(operator == 0 && num1 == 0) {
                    num1 = Double.parseDouble(display.getText());
                    operator = 2;
                    fakedisplay.setText("");
                } else if(operator != 0) {
                    operator = 2;
                    num2 = Double.parseDouble(fakedisplay.getText());
                    fakedisplay.setText("");
                    if(operator == 1) {
                        total = num1 + num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 2) {
                        total = num1 - num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 3) {
                        total = num1 * num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    if(operator == 4) {
                        total = num1 / num2 + num1 % num2;
                        display.setText(display.getText() + operation + total);
                        num1 = total;

                    }
                    num2 = 0;
                    total = 0;
                    fakedisplay.setText("");
                    operator = 0;
                }
                operation = "-";
                display.setText(display.getText() + operation);

            } else if(e.getSource() == equals) {
                if(operator == 0) {
                    display.setText("Error");
                } else if(operator != 0) {
                    num2 = Double.parseDouble(fakedisplay.getText());
                    fakedisplay.setText("");
                    if(operator == 1) {
                        total = num1 + num2;
                        display.setText(display.getText() + "=" + total);
                    }
                    if(operator == 2) {
                        total = num1 - num2;
                        display.setText(display.getText() + "=" + total);
                    }
                    if(operator == 3) {
                        total = num1 * num2;
                        display.setText(display.getText() + "=" + total);
                    }
                    if(operator == 4) {
                        total = num1 / num2 + num1 % num2;
                        display.setText(display.getText() + "=" + total);
                    }

                }
                operation = "=";

            } else if(e.getSource() == clear) {
                display.setText("");
                fakedisplay.setText("");
                operator = 0;
                total = 0;
                num1 = 0;
                num2 = 0;


            }
            System.out.println("num1: " + num1 + " num2: " + num2 + "\ndisplay: " + display.getText() + " fakedisplay: " + fakedisplay.getText() + "\nresponse: " + response + "\noperator: " + operator + "\ntotal: " + total + "\n==========================");

        }



    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2555459
  • 29
  • 2
  • 6
  • 1
    Then you need to add a "compute" or "=" button to let it see all operators with relevant priority. Like writing a mini compiler's parser. Doing the +-/* immediately changes the order of compute. You need priorities. So move the computing lines to the "=" button please. – huseyin tugrul buyukisik Jul 15 '13 at 07:43
  • 8*2 was a bad example, it takes the second operator and uses it for the first unlike when theres just 2 numbers. – user2555459 Jul 15 '13 at 07:47
  • Welcome to Stack Overflow. Please note that this is not "please-debug-my-code" site. People ask questions and receive answers here. Your post cannot be called "question". – AlexR Jul 15 '13 at 07:47
  • could you possibly edit the code so i can fully understand? @huseyin – user2555459 Jul 15 '13 at 07:48
  • @alexr yeah the question is why is my program not working correctly – user2555459 Jul 15 '13 at 07:49
  • Just leave display.setText() in button body because doing calc in every actionPerformed is bad. Concatenate strings the way you do, just do the calc only in "equal" button is pressed. – huseyin tugrul buyukisik Jul 15 '13 at 07:52
  • well i like keeping it up to date as you go along, i wanted it like a real calculator to keep showing the history of what you did – user2555459 Jul 15 '13 at 07:54
  • 1
    Unless there is a specification to do it a particular way (e.g. learning purposes), I'd use the [`ScriptEngine`](http://docs.oracle.com/javase/7/docs/api/javax/script/ScriptEngine.html) for this functionality. E.G. [Calculet](http://stackoverflow.com/a/7441804/418556). – Andrew Thompson Jul 15 '13 at 07:57
  • no specification, im doing solely for learning purposes, its my first project with GUI so im not familiar with scriptengine really. – user2555459 Jul 15 '13 at 07:58
  • @Andrew This script engine can parse java commands too? – huseyin tugrul buyukisik Jul 15 '13 at 08:00
  • *"This script engine can parse java commands too?"* No. But it can parse equations, create and use objects, and do many of the other basic things that Java can do. There is more to the `ScriptEngine` than simply `eval` (as complex as that is!). – Andrew Thompson Jul 15 '13 at 08:02
  • @Andrew, added your suggestion in the answer in your name. – huseyin tugrul buyukisik Jul 15 '13 at 08:03

2 Answers2

0

I can only guide you to some online information, you need to work on the parsing. Put each token(+-*/) of final command string in a list(maybe a binary tree works) and sort that list to tell the calculator that division and multiplication has the highest priority in the mean time addition and subtraction has lower priority.

You sort the command array in such way that there is also priority of leftside against rightside. Then it starts calculating from left to right while selecting div/mult first.

http://www.slideshare.net/dabeaz/writing-parsers-and-compilers-with-ply

http://parsingintro.sourceforge.net/

http://www.dreamincode.net/forums/topic/268945-an-introduction-to-compiler-design-part-ii-parsing/

https://en.wikipedia.org/wiki/Parsing

http://arantxa.ii.uam.es/~modonnel/Compilers/03_1_Parsing_Intro.pdf

If you are too tired, you can use the script engine of java, a suggestion from user "Andrew Thompson".

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
0

I think You may be getting a littler different way. You are designing a complex solution for a very simple problem.

1. First of all Use  a single JTextField for the user to enter the entire expression.
   i.e. [ 4+4*2].

2. Second Provide a Submit Buttons( jbutton ) along with text field. 
   Now when the User is finished with entering expression he will click the submit button.

   i.e [4+4*2] [Click to Submit]

No you write your handler for this button. in the handler just read the whole expression from the text field and follow Reverse Polish Notation to evaluate your result. Its avery simple Stack based algorithm. Generally taught in data structure classes. see this article http://en.wikipedia.org/wiki/Reverse_Polish_notation

veritas
  • 2,444
  • 1
  • 21
  • 30