On 10/15/12 10:19 AM, John B. Matthews wrote:
In article <3a63483c-4322-4bfb-8c28-2d528bf48443@googlegroups.com>,
William Lopes <williamlopes.dev@gmail.com> wrote:
I have to do a conversion between String and Double object, but my
string is a mathematical operation like "100 + 10". Even when I
make a conversion using NumberFormat.getInstance of "100 + 10", my
result is 100.0 only.
I would like to do it without split my string of way manually.
Your distribution may already contain a suitable implementation of
javax.script.ScriptEngine:
ScriptEngineManager mgr = new ScriptEngineManager();
List<ScriptEngineFactory> factories = mgr.getEngineFactories();
for (ScriptEngineFactory f : factories) {
System.out.println(f);
}
Selecting the available RhinoScriptEngine by extension
ScriptEngine engine = mgr.getEngineByExtension("js");
try {
System.out.println(engine.eval("5 * 8 + 2"));
} catch (ScriptException ex) {
ex.printStackTrace(System.err);
}
prints the expected answer, 42.0.