Is single line calculator possible? For example, if I input 1 + 2 + 10 - 5 * 3, it will calculate it and display the result.
Asked
Active
Viewed 252 times
-3
-
3yes, it is possible. – iPirat Oct 30 '18 at 09:02
-
1And beware of `(` and `)` for a single line calculator – Dang Nguyen Oct 30 '18 at 09:04
-
If you mean with a single semicolon, I'm not sure that it is. – Boris the Spider Oct 30 '18 at 09:04
-
you are new to this site, please take your time to read https://stackoverflow.com/help/how-to-ask – iPirat Oct 30 '18 at 09:04
-
Anything can be done, if you imagine! Yes it can be done not only with java but other languages as well. Just need to code it correctly to process math operations in the correct order. – c-chavez Oct 30 '18 at 09:05
-
1To give you a great answer, it might help us if you have a glance at [ask] if you haven't already. It might be also useful if you could provide a [mcve]. – Mat Oct 30 '18 at 09:07
1 Answers
1
public static String calc(String expr) throws ScriptException {
return String.valueOf(new ScriptEngineManager().getEngineByName("JavaScript").eval(expr));
}

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