i have a string that has sum numbers and space and som '+'and'-' characters between them .i want to now How can summand and subtract them.!! like String s="25+14-13+225-16"; and i want to output to be =235
Asked
Active
Viewed 69 times
-5
-
Please show us this `String` and your effort on how to resolve the problem... You will have to split the `String` and handle the parts separately. – deHaar Apr 26 '19 at 10:33
-
Please [edit] your question with the code you're having problems with – Lino Apr 26 '19 at 10:33
1 Answers
0
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
public class Test {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));
}
}

Shweta
- 219
- 5
- 18