aspose file tools*
The moose likes Java in General and the fly likes need to calculate formulae given as string expression. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "need to calculate formulae given as string expression." Watch "need to calculate formulae given as string expression." New topic
Author

need to calculate formulae given as string expression.

john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
I have two String expression of a formulae like

String s = "max(1, 5)" ;

and
int a=2;
int b=2
String s2 = "a * b";

Is there any API in java which takes these string and calculate it as a mathematical expression?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
Search the FAQ pages for a library named JEP; it does this.


Ping & DNS - updated with new look and Ping home screen widget
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
It is not a freeware ...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

If the formulas are not too complex you could try to use a JavaScript engine. See http://download.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html for more information.

Edit: I just tried it, and it can handle a * b just fine:
The max function is undefined in JavaScript however. You can solve that by adding it as a function after line 8:
You may want to add a few similar functions like that, like min.

Also, the eval method returns a Double, but the cast to Number and then calling its intValue() method is a bit safer to get the int result.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
john sal wrote:It is not a freeware ...

It is, if you use the version linked in the JavaRanch FAQ (which is GPLed).
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
can you please guide me to the link for GPLed as I am not able to find it..

Thanks.
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
Hi Rob thanks a lot it worked fine and is vry helpful ..... Just have a doubt how can we make it work for the following expression?

"max(a,b)/c"

The only way i found is to send the c as well as an argument and do all the calculations in the method itself..is there any other possible and better way of doing this?

john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
Hey Rob thanks ... It is working fine for this expression as well.....I was executing it in a wrong way hence was not getting the expected result....

Thanks a lot
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need to calculate formulae given as string expression.
 
Similar Threads
((A)b).x what kind of construct is this?
Function Overloading
function call
calculate a string expression
Averaging Grades Assignment