Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

dealing with formula objects  RSS feed

 
Bernard Grimshaw
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have an application where I need a user to enter a formula into a text box that will be saved to a database

eg (a+b) but potentially more complex

It would normally be stored as String object

I would later like to output a report using this formula

Ideally

double a = 20;
double b= 10;
double c = formula;

This obviously cant work as the formula is not perceived as a formula but as a string. Is there a simple way of converting the string to a kind of object that will be treated as a calculation

Hope I've made this clear

Thanks
Bernard
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Java 6 then you can use the scripting API. The expression has to be a valid expression in whichever scripting language you choose to use. An engine for JavaScript is included with Java 6. Example:

NOTE: I had to put a space in the word "eva l" because the JavaRanch forum software considers that to be a "dangerous" word... (someone could use that to post a malicious script in a post...). Remove the space to make it work.

If you are using an older version of Java (the scripting engine is new to Java 6), then have a look at Rhino. (The scripting engine in Java 6 is actually a version of Rhino).
[ October 31, 2007: Message edited by: Jesper Young ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the best value for an employer's dollar, definitely look for a scripting solution or an open source evaluation library.

For pure geek entertainment and a great learning experience, it's a common computer science assignment to write such a thing. Google for "infix to rpn" conversion algorithms. It's a short trip from RPN to computing the expression. I wrote an RPN evaluator in my formative years, and I was a music major.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JavaIntermediateFaq links to a few libraries that can evaluate expressions.
 
Bernard Grimshaw
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a really interesting approach. I'm not using java 6 yet but am planning to upgrade. I will definitely try it out

Thanks a lot

Bernard
 
John O'Hanley
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a commecial tool called formula4j that was built for this purpose.

One disadvantage of eval and scripting tools is that they are open to injection attacks.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bernard Grimshaw wrote:Thats a really interesting approach...

I assume you're referring to Stan's post (because he said exactly what I would have).

It should be added that it works fine for expressions involving arithmetic operators (ie, the ones given to you by Java) and perhaps the functions offered by the Math class. If you want to be able to define your own functions, then it gets more fun; but still very doable.

Good old RPN. Makes me proud to be a Gutkowski.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!