Search code examples
javaswingjlabeljtextfieldscriptengine

Running Non-Standard Evaluations based of input from JTextField (sin, cos, tan)


I am writing a program that utilizes the provided library ScriptEngineManager to try and write a math calculator that goes beyond the basic functions of +, -, / and *. How would I be able to go about implementing this in the existing code.

I have already attempted using .replace, although it's possible that I have implemented it wrong.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class GUIRunner {

    public static void main(String args[]) {
        JFrame f = new JFrame("megamath1.0");
        f.setSize(300,300); 
        f.setLocation(100,150);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFrame.setDefaultLookAndFeelDecorated(true);
        JLabel labelM = new JLabel("Enter Equation: ");
        labelM.setBounds(50, 50, 200, 30);
        JLabel label = new JLabel(); 
        label.setBounds(50,100,200,30);
        label.setVisible(true);
        JTextField motto1 = new JTextField();
        //set size of the text box
        motto1.setBounds(50, 100, 200, 30);
       //add elements to the frame
        f.add(labelM);
        f.add(motto1);
        f.setLayout(null);
        f.setVisible(true);
        JButton b = new JButton("Submit");
        b.setBounds(50, 150, 100, 30);
        b.addActionListener(new ActionListener() {
         @Override
            public void actionPerformed(ActionEvent arg0) {
                String inputText = motto1.getText(); 

            ScriptEngineManager mgr = new ScriptEngineManager();
            ScriptEngine engine = mgr.getEngineByName("JavaScript");
                try {
                motto1.setText("Output: " + engine.eval(inputText));
                } catch (ScriptException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

       });
       //add button to the frame
       f.add(b);
       f.add(label);
}
}

It currently does work for standard calculations such as the ones provided above (+, -, *, /) by simply changing the text in the text field to the proper answer. How could I implement more complicated mathematics


Solution

  • So at the moment you only put the input text into a JavaScript engine and hope that the thing works. If you really want to stick to this approach, the user has to know JavaScript to use the calculator. For example if you input Math.sin(42) you will get the sine value of 42.

    If you want to implement a more convenient math syntax similar to real math you need to write your own script engine for the math expressions. There are libraries that can help with this, but you'll need a good understanding of what you are doing.