Posted In: Java Core, String

How to parse a mathematical expression given as a string – using ScriptEngine

Evaluating a math expression given in string form package com.example.mathexpression; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import org.junit.Test; public class MathExpression2 { @Test public void evaluate1() throws Exception { System.out.println(“\nevaluate1() start”); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName(“JavaScript”); String result = “5+4*(7-15)”; System.out.println(engine.eval(result)); } }

Read More

Tags:

by , on August 30th, 2017

Posted In: Java Core, String

How to parse a mathematical expression given as a string – using Bean Shell

Evaluating a math expression given in string form Maven beanshell beanshell import package com.example.mathexpression; import org.junit.Test; import bsh.Interpreter; public class MathExpression { @Test public void evaluate1() throws Exception { System.out.println(“\nevaluate1() start”); Interpreter interpreter = new Interpreter(); interpreter.eval(“result = 5+4*(7-15)”); System.out.println(interpreter.get(“result”)); } }

Read More

Tags:

by , on August 30th, 2017

  • Categories