How can I solve the line version of the calculation for a simple calculator?

It is a simple calculator where the user can enter a calculation and click enter, and the calculator will determine if it is a valid calculation or not. If the calculation is correct, the calculation is performed. If not, an error message will be displayed.

The part calculation is carried out

is incomplete.

Can anyone suggest solutions for the method getAnswer()

.

It would be very grateful.

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


@SuppressWarnings("serial")
public class Calculator extends JFrame{

    private interface CalculatorInterface {
        public void writeToScreen(String text);
        public void clearScreen();
        public String getScreenText();  
    }

    private class CalculatorPanel extends JPanel implements CalculatorInterface {

        private class NumberPanel extends JPanel implements CalculatorInterface {

            private static final int NUMTOTAL = 10;

            private CalculatorPanel calcPanel;
            private JButton[] numButtons;

            public NumberPanel(CalculatorPanel calcPanel) {
                this.calcPanel = calcPanel;
                buildLayout();
                addButtons();
            }
            private void buildLayout() {
                this.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

                GridLayout layout = new GridLayout(4,3);
                layout.setHgap(1);
                layout.setVgap(1);

                this.setLayout(new GridLayout(4,3));

            }
            private void addButtons() {
                numButtons = new JButton[NUMTOTAL];

                for(int i = numButtons.length -1; i >= 0 ; i--) {
                    numButtons[i] = new JButton("" + i);
                    numButtons[i].setPreferredSize(new Dimension(60,40));
                    numButtons[i].setFont(new Font("Sans serif", Font.PLAIN, 18));
                    numButtons[i].addActionListener(
                            new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    String text = ((JButton)e.getSource()).getText().trim();
                                    if(getScreenText().equals("Invalid Calc")) {
                                        clearScreen();
                                        writeToScreen(text);
                                    }else {
                                        writeToScreen(text);
                                    }       
                                }
                            });
                    this.add(numButtons[i]);
                }
            }
            @Override
            public void writeToScreen(String text) {
                calcPanel.writeToScreen(text);
            }
            @Override
            public void clearScreen() {
                calcPanel.clearScreen();

            }
            @Override
            public String getScreenText() {
                return calcPanel.getScreenText();
            }

        }

        private class OperPanel extends JPanel implements CalculatorInterface {

            private static final int ADD = 0;
            private static final int SUB = 1;
            private static final int MULT = 2;
            private static final int DIV = 3;
            private static final int OPENB = 4;
            private static final int CLOSEB = 5;
            private static final int CLEAR = 6;
            private static final int EQL = 7;


            private static final int OPERTOTAL = 8;

            private CalculatorPanel calcPanel;
            private JButton[] operButtons;

            public OperPanel(CalculatorPanel calcPanel) {
                this.calcPanel = calcPanel;
                buildLayout();
                addButtons();
            }

            private void buildLayout() {
                GridLayout layout = new GridLayout(4,1);
                layout.setHgap(1);
                layout.setVgap(1);

                this.setLayout(new GridLayout(4,1));
            }

            private void addButtons() {
                operButtons = new JButton[OPERTOTAL];

                operButtons[ADD] = makeButton(ADD, "+");
                operButtons[SUB] = makeButton(SUB, "-");
                operButtons[MULT] = makeButton(MULT, "*");
                operButtons[DIV] = makeButton(DIV, "/");
                operButtons[CLEAR] = makeButton(CLEAR, "CL");
                operButtons[EQL] = makeButton(EQL, "=");
                operButtons[OPENB] = makeButton(OPENB, "(");
                operButtons[CLOSEB] = makeButton(CLOSEB, ")");

                for(JButton button: operButtons) {
                    this.add(button);
                }   
            }

            private JButton makeButton(int index, String label) {   

                operButtons[index] = new JButton(label);
                operButtons[index].addActionListener(
                        new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                String text = ((JButton)e.getSource()).getText();
                                if(text.equals("=")) {
                                    String screenText = getScreenText();
                                    clearScreen();
                                    try {
                                        writeToScreen(getAnswer(screenText));
                                    }catch(Exception excep) {
                                        writeToScreen("Invalid Calc");
                                    }
                                }else if(text.equals("CL")) {
                                    clearScreen();  
                                }else {
                                    writeToScreen(text);
                                }
                            }       
                        });

                return operButtons[index];      
            }


            private String getAnswer(String text) throws Exception {
                /*I'm trying to solve for any input by the user e.g
                 *(the stuff in square brackets represents what is displayed
                 * on the screen:.
                 *[1+1] (hits equals) [2]
                 *[1+2-3] (hits equals) [0]
                 *[1+2*3] (hits equals) [7]
                 *[10*(14+1/2)] (hits equals) [145]
                 */
                throw new Exception();
            }
            @Override
            public String getScreenText() {
                return calcPanel.getScreenText();
            }

            @Override
            public void clearScreen() {
                calcPanel.clearScreen();

            }

            @Override
            public void writeToScreen(String text) {
                calcPanel.writeToScreen(text);

            }

        }


        private NumberPanel numPanel;

        private OperPanel operPanel;
        private JTextField calcScreen;

        public CalculatorPanel(JTextField calcScreen) {
            this.calcScreen = calcScreen;

            buildNumPanel();
            buildOperPanel();

            buildCalcPanel();


        }
        private void buildNumPanel() {
            this.numPanel = new NumberPanel(this);
        }

        private void buildOperPanel() {
            this.operPanel = new OperPanel(this);
        }

        private void buildCalcPanel() {
            this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
            this.add(numPanel);
            this.add(operPanel);
        }


        @Override
        public void writeToScreen(String text) {
            calcScreen.setText(getScreenText() + text);     
        }

        @Override
        public String getScreenText() {
            return calcScreen.getText();
        }

        @Override
        public void clearScreen() {
            calcScreen.setText("");
        }
    }

    private JPanel mainPanel;
    private JTextField calcScreen;

    private CalculatorPanel calcPanel;
    public Calculator() {
        buildScreen();

        buildCalcPanel();

        buildMainPanel();

        buildCalculator();
    }

    private void buildScreen() {
        this.calcScreen = new JTextField();
        this.calcScreen.setPreferredSize(new Dimension(150,50));
        this.calcScreen.setHorizontalAlignment(JTextField.CENTER);
        this.calcScreen.setFont(new Font("Sans serif", Font.PLAIN, 30));

    }

    private void buildCalcPanel() {
        this.calcPanel = new CalculatorPanel(this.calcScreen);
    }

    private void buildMainPanel() {
        this.mainPanel = new JPanel();
        this.mainPanel.setBorder(new EmptyBorder(10,10,10,10));
        this.mainPanel.setLayout(new BoxLayout(this.mainPanel, BoxLayout.Y_AXIS));

        this.mainPanel.add(calcScreen);
        this.mainPanel.add(calcPanel);
    }

    private void buildCalculator() {

        this.add(mainPanel);
        this.setTitle("Calculator");
        this.pack();        
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

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

}

      

How can I check if a string is a valid calculation for a simple calculator?

Edit 1: Fixed a silly bug in the makeButton () method where I passed the text of the button to be checked instead of the text on the screen. (I'm an idiot.)

Edit 2: Remove the isValid (line text) code from the code and make the getAnswer () method just throw an exception if the input is invalid.

+3


source to share


1 answer


As mentioned in the previous answer StackOverflow

( Evaluating a math expression given in string form ), you can use Javascript's

ScriptEngine

to evaluate expressions based on strings that you extracted from a textbox. First place it in try-catch block

to see if there is an error in the expression. In the catch block, set a variable to keep its valid expression or not.

boolean validExpression = true;

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String input = textField.getText() // Modify this to whatever variable you have assigned to your text field

try {
    System.out.println(engine.eval(foo));
} 
catch (ScriptException e) {
    validExpression = false;
    System.out.println("Invalid Expression");
}

      

Make sure you include the following imports:



import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

      

While you can try to implement the Shunting-Yard algorithm or another arithmetic parser, this is simply a more pragmatic solution.

+3


source







All Articles