0

Below I have just the function to do the math for each evaluation, but for some reason, my numbers aren't appearing in the text field as I'm pushing them, which means its not adding them to my string. Is there anything noticeable missing that would cause this? When I run, I do get the message saying wrong input. Thanks in advance!

private double eval(final String str) {
    class Parser {
        int pos = -1, c;

        void eatChar() {
            c = (++pos < str.length()) ? str.charAt(pos) : -1;
        }

        void eatSpace() {
            while (Character.isWhitespace(c)) eatChar();
        }

        double parse() {
            eatChar();
            double v = parseExpression();
            if (c != -1) throw new RuntimeException("Unexpected: " + (char)c);
            return v;
        }

        // Grammar:
        // expression = term | expression `+` term | expression `-` term
        // term = factor | term `*` factor | term `/` factor | term brackets
        // factor = brackets | number | factor `^` factor
        // brackets = `(` expression `)`

        double parseExpression() {
            double v = parseTerm();
            for (;;) {
                eatSpace();
                if (c == '+') { // addition
                    eatChar();
                    v += parseTerm();
                } else if (c == '-') { // subtraction
                    eatChar();
                    v -= parseTerm();
                } else {
                    return v;
                }
            }
        }

        double parseTerm() {
            double v = parseFactor();
            for (;;) {
                eatSpace();
                if (c == '/') { // division
                    eatChar();
                    v /= parseFactor();
                } else if (c == '*' || c == '(') { // multiplication
                    if (c == '*') eatChar();
                    v *= parseFactor();
                } else {
                    return v;
                }
            }
        }

        double parseFactor() {
            double v;
            boolean negate = false;
            eatSpace();
            if (c == '(') { // brackets
                eatChar();
                v = parseExpression();
                if (c == ')') eatChar();
            } else { // numbers
                if (c == '+' || c == '-') { // unary plus & minus
                    negate = c == '-';
                    eatChar();
                    eatSpace();
                }
                StringBuilder sb = new StringBuilder();
                while ((c >= '0' && c <= '9') || c == '.') {
                    sb.append((char)c);
                    eatChar();
                }
                if (sb.length() == 0) throw new RuntimeException("Unexpected: " + (char)c);
                v = Double.parseDouble(sb.toString());
            }
            eatSpace();
            if (c == '^') { // exponentiation
                eatChar();
                v = Math.pow(v, parseFactor());
            }
            if (negate) v = -v; // exponentiation has higher priority than unary minus: -3^2=-9
            return v;
        }
    }
    return new Parser().parse();
}

public void actionPerformed(ActionEvent e){
    String input = ((JButton)e.getSource()).getText();

    if(input.equals("C")){
        formula = "";
    }else if(input.equals("=")){
        try{
            double result = eval(formula);
            formula = "" + result;
        } catch(RuntimeException re){
            JOptionPane.showMessageDialog(null, "Wrong input: " + formula);
            formula = "";
        }
        }else{
            formula += input;
    }
        eqDisplay.setText(formula);
}

public static void main(String[] args) {
    Calculator calc = new Calculator();
}
}

Update: Here's the layout too.

public class Calculator extends JFrame implements ActionListener {  
String formula;
JTextField eqDisplay;

public Calculator() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(new Dimension(230, 250));
    this.setTitle("Calculator");
    this.setLayout(new BorderLayout());

    formula = "";

    JPanel centerPanel = new JPanel(new GridBagLayout());
    for (int i = 1; i <= 9; i++) {
        GridBagConstraints constraint = new GridBagConstraints();
        constraint.gridx = (i-1)%3;
        constraint.gridy = (i-1)/3;
        constraint.insets = new Insets(5, 5, 5, 5);
        JButton b = new JButton("" + i);
        centerPanel.add(b, constraint);
    }

    GridBagConstraints constraint = new GridBagConstraints();
    constraint.insets = new Insets(5, 5, 5, 5);

    constraint.gridx = 0;
    constraint.gridy = 3;
    constraint.gridwidth = 2;
    constraint.fill = GridBagConstraints.BOTH;
    JButton num_0 = new JButton("0");
    centerPanel.add(num_0, constraint);
    num_0.addActionListener(this);

    constraint.gridx = 2;
    constraint.gridy = 3;
    constraint.gridwidth = 1;
    constraint.fill = GridBagConstraints.NONE;
    JButton point = new JButton(".");
    centerPanel.add(point, constraint);
    point.addActionListener(this);

    constraint.gridx = 3;
    constraint.gridy = 0;       
    JButton divide = new JButton("/");
    centerPanel.add(divide, constraint);
    divide.addActionListener(this);

    constraint.gridx = 3;
    constraint.gridy = 1;       
    JButton multiply = new JButton("*");
    centerPanel.add(multiply, constraint);
    multiply.addActionListener(this);

    constraint.gridx = 3;
    constraint.gridy = 2;   
    JButton minus = new JButton("-");
    centerPanel.add(minus, constraint);
    minus.addActionListener(this);

    constraint.gridx = 3;
    constraint.gridy = 3;       
    JButton plus = new JButton("+");
    centerPanel.add(plus, constraint);
    plus.addActionListener(this);

    constraint.gridx = 0;
    constraint.gridy = 4;       
    constraint.gridwidth = 3;
    constraint.fill = GridBagConstraints.BOTH;

    JButton result = new JButton("=");
    centerPanel.add(result, constraint);
    result.addActionListener(this);

    constraint.gridx = 3;
    constraint.gridy = 4;       
    constraint.gridwidth = 1;
    constraint.fill = GridBagConstraints.NONE;

    JButton cButton= new JButton("C");
    centerPanel.add(cButton, constraint);
    cButton.addActionListener(this);

    this.add(centerPanel, BorderLayout.CENTER);
    JPanel northPanel = new JPanel(new FlowLayout());        
    eqDisplay = new JTextField(15);
    northPanel.add(eqDisplay);
    this.add(northPanel, BorderLayout.NORTH);

    this.setVisible(true);
}
Terik Brunson
  • 187
  • 1
  • 13
  • 1
    You're using `==` to compare strings in your `actionPerformed` method. Check the question that I linked this to, if you're unsure why this is a bad idea. – Dawood ibn Kareem Feb 08 '15 at 19:34
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *objects* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Feb 08 '15 at 19:35
  • Changed that... still the same problem. I understand why it's bad, but my professor wrote that code in an example. It should be working regardless, I must be missing something else. – Terik Brunson Feb 08 '15 at 19:42
  • 1
    (1) I have reopened the question. (2) Your professor should be doing a different job. It's unacceptable for someone to make a beginner error like this while attempting to teach others how to program. (3) You could try stepping through this with a debugger, if you want to find out what's going on. You'll be able to see exactly which lines of your program are running, in response to each possible event. – Dawood ibn Kareem Feb 08 '15 at 20:07
  • The problem was in my layout. When I used the loop to create buttons I wasn't adding an actionListener to each one. Thanks for the help (: – Terik Brunson Feb 10 '15 at 20:09
  • It's probably a good idea to add that as an answer. It's OK on here to answer your own question if you figure it out - that way it's available for others if they run across similar problems. – J Richard Snape Feb 10 '15 at 20:29

0 Answers0