-1

As you'll probably be able to tell by my way of doing things, I'm a beginner when it comes to Java. Basically I have something like this:

 String math = "1 + 1";

I want to be able to execute that math equation so that it will return the value 2. Something presumably like this:

 String math = "1 + 1";
 int answer = Math(math);
 System.out.print(answer);

How would I go about doing this? Thanks in advance.

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
user2879373
  • 73
  • 1
  • 1
  • 10

1 Answers1

0

use beanshell

http://www.beanshell.org/manual/bshmanual.html#eval%28%29

Object result = i.eval("1 + 1");  // Integer
Leo
  • 6,480
  • 4
  • 37
  • 52