-3

Is single line calculator possible? For example, if I input 1 + 2 + 10 - 5 * 3, it will calculate it and display the result.

1 Answers1

1
public static String calc(String expr) throws ScriptException {
    return String.valueOf(new ScriptEngineManager().getEngineByName("JavaScript").eval(expr));
}

See How can I run JavaScript code at server side Java code?

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • A bad solution for reasons described here: https://stackoverflow.com/a/27396372/545127 – Raedwald Oct 30 '18 at 09:14
  • @Raedwald agree. This is not good, but not bad. It works with numeric expression. Probably for other inputs this is not correct or ever dangerous. – Oleg Cherednik Oct 30 '18 at 09:17