Mohamed Magdy Hassan
Mohamed Magdy Hassan

Reputation: 105

Can I convert a string to a math operation in java?

can I convert a string like "3*3+3" to math operation in java??

Upvotes: 7

Views: 4624

Answers (2)

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46728

Evaluate it is as JavaScript using ScriptEngine

String xyz = "3*3+3";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName("JavaScript");        
Object result = se.eval(xyz);

Reference: Documentation

Upvotes: 12

Elchin
Elchin

Reputation: 604

There is no built-in function for that, you would have to implement a parser. However you could also look up for ready project, such as: http://sourceforge.net/projects/jep/ or http://code.google.com/p/symja/wiki/MathExpressionParser

Upvotes: 3

Related Questions