-2

so I am looking for advice on how to solve a calculation that is entered as a String in Java.

For example: "50 * 6 - 75 / 25" is entered I am then needing to solve this calculation using bodmas to.

So (50*6)-(75/25)

Answer = 297

I didn't explain it very well but any help would be appreciated.

user2849651
  • 13
  • 1
  • 7

1 Answers1

0

You can use the BeanShell's Interpreter class to do this.

new Interpreter().eval("50 * 6 - 75 / 25");

Or you can also use the java's inbuilt JavaScript engine, an example of which is shown below

Evaluating a math expression given in string form

Community
  • 1
  • 1
anirudh
  • 4,116
  • 2
  • 20
  • 35