Now I hava a String lam = "(8-2*7.5)+10"
,so is there any method or class to calculate this String expression and return me the result directly?
Thanks in advance!
Now I hava a String lam = "(8-2*7.5)+10"
,so is there any method or class to calculate this String expression and return me the result directly?
Thanks in advance!
try this
double res = (Double) new ScriptEngineManager().getEngineByName("js").eval("(8-2*7.5)+10");
java 1.6 provides Javascript engine within. You can use that to calculate your mathematical expression.
clases to be used are : ScriptEngineManager
and ScriptEngine
Almost certainly not by default. This would correspond to either runtime interpretation of Java code (eeek!) or parsing and interpretation machinery for a fairly trivial arithmetic sub-language embedded within the Java standard library.
If you need to parse and evaluate expressions of this sort, I would consider writing a simple recursive-descent parser and evaluating on-the-fly. A quick Google search should suggest ways to do this in Java for languages of precisely this sort.