如何在Java中求解字符串中的基本数学方程?

如何在Java中求解字符串中的基本数学方程?,java,jframe,jbutton,actionlistener,scriptengine,Java,Jframe,Jbutton,Actionlistener,Scriptengine,我有一个基本的计算器,当按下equal按钮时,JTextField会以字符串形式保存,这就是问题所在,它是字符串形式的,所以我不知道如何解方程。我尝试过使用JavaScript引擎,但无法解决这个问题。有什么帮助吗 //actionListener @Override public void actionPerformed(ActionEvent e) { if(e.getSource().equals(oneButton)){ input1 = 1;

我有一个基本的计算器,当按下equal按钮时,JTextField会以字符串形式保存,这就是问题所在,它是字符串形式的,所以我不知道如何解方程。我尝试过使用JavaScript引擎,但无法解决这个问题。有什么帮助吗

//actionListener
@Override
public void actionPerformed(ActionEvent e) {

    if(e.getSource().equals(oneButton)){
        input1 = 1;
        text.setText(text.getText()+input1);}
    else if (e.getSource().equals(twoButton)){
        input2 = 2;
        text.setText(text.getText()+input2);}
    else if(e.getSource().equals(threeButton)){
        input3 = 3; 
        text.setText(text.getText()+input3);}
    else if(e.getSource().equals(fourButton)){
        input4 = 4; 
        text.setText(text.getText()+input4);}
    else if(e.getSource().equals(fiveButton)){
        input5 = 5;
        text.setText(text.getText()+input5);}
    else if(e.getSource().equals(sixButton)){
        input6 = 6;
        text.setText(text.getText()+input6);}
    else if(e.getSource().equals(sevenButton)){
        input7 = 7;
        text.setText(text.getText()+input7);}
    else if(e.getSource().equals(eightButton)){
        input8 = 8;
        text.setText(text.getText()+input8);}
    else if(e.getSource().equals(nineButton)){
        input9 = 9;
        text.setText(text.getText()+input9);}
    else if(e.getSource().equals(zeroButton)){
        input0 = 0;
        text.setText(text.getText()+input0);}
    else if(e.getSource().equals(plusButton)){
        text.setText(text.getText()+" + ");}
    else if(e.getSource().equals(minusButton)){
        text.setText(text.getText()+" - ");}
    else if(e.getSource().equals(timesButton)){
        text.setText(text.getText()+" * ");}
    else if(e.getSource().equals(dividButton)){
        text.setText(text.getText()+" / ");}
    else if(e.getSource().equals(clrButton)){
        text.setText("");}
    else if(e.getSource().equals(enterButton)){
        eq = text.getText();
//trying the javascript     ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");

            try {
                text.setText((String) engine.eval(eq));
            } catch (ScriptException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

    }

您可以编写一个计算器,使用antlr解析字符串:

为什么不使用Integer和Double类中的parse方法呢

int integerNumber = Integer.parseInt(text.getText());
double doubleNumber = Double.parseDouble(text.getText());

如您所见,Integer类中的parseInt()方法将字符串转换为整数,Double也会发生同样的情况。完成此操作后,您可以完美地对字符串进行任何计算。

如果操作顺序很重要,您将不得不使用堆栈。如果你只是从左到右计算,你应该没事

当@Ericson Willians回答时,您可以通过以下方式将字符串转换为int或double:

    String str = text.getText();
    Integer.parseInt(str);
    Double.parseDouble(str);
但是,在这里您可能会遇到麻烦,因为当您按下enter按钮时,您将得到放置在文本元素中的整个等式,也就是说,如果您按下enter键,并且将等式存储为“2+3”,则text.getText()将为您提供“2+3”如果不给您一个错误,您将无法使用以下内容

    Integer.parseInt(text.getText()); //NumberFormatException For input string: "2 + 3"
    Double.parseDouble(text.getText()); //NumberFormatException for "2 + 3"
在将字符串转换为整数或双精度之前,必须以特定的方式分解字符串。分解字符串的一个好方法是使用以下内容:

String st = text.getText();
//Example: String st = "2 + 3";
//remove white space
st = st.replaceAll("\\s","");
//might have to change to allow for negative numbers
String[] splitStrings = (st.split("((?<=[+-/*])|(?=[+-/*]))")); 
//Example: splitStrings is now -> ["2","+","3"]
但是,由于等式可能很大,您可能应该创建一个循环来执行类似的操作,并且您需要一个if语句或switch语句来执行所有操作

int result;
int left = Integer.parseInt(splitStrings[0]);
String op = splitStrings[1];
int right = Integer.parseInt(splitStrings[2]);
if(op.equals("+")){
    result = left + right;
}
text.setText("" + result);