Isn't there a way to calculate an expression in the form of a string?

Asked 2 years ago, Updated 2 years ago, 144 views

string java math

2022-09-21 20:50

1 Answers

JDK1.6 and above can be done simply by using JavaScript Enzil.

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

public class Test {
  public static void main(String[] args) throws Exception{
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    String foo = "40+2";
    System.out.println(engine.eval(foo));
    } 
}


2022-09-21 20:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.