简体   繁体   中英

Does anyone know how to use Expression in Android Studio?

I'm making a calculator in Android Studio and am facing a problem.

Here is my code:

String input = getinput();
try {
   if (!isEmpty() && !stateError) {
      if (input.contains("x")){
          input.replaceAll("x","*");
      }
      Expression expression = new ExpressionBuilder(input).build();
   }
} catch (Exception e) { }

When I type Expression , it shows the error:

"Cannot resolve symbol 'Expression'."

Is there any solution to solve this problem?

I don't know if you have found this or not, but there is this site that I found that seems to be the API for Expression and ExpressionBuilder called exp4j.

If that is in fact the API, then this is the download link for it.


Another way to evaluate an equation from a String in Java is to use the ScriptEngine interface along with the ScriptEngineManager class. You can use it like the following:

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("javascript");

try {
    String result = engine.eval(stringEquation).toString();
    System.out.println("Result: "+result);
} 
catch (ScriptException se) {
    // Handle error
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM