How to evaluate math expression in java:

Some times ago I faced a problem that I need to map some fields from one object to other fields in another object.

It's not very sophisticated problem but the main trouble was that sometimes I had to do some transformation with that fields. For example I have to set to field w/ name "account" value created as sum from field "text" and "senderSubId". Or sometimes I have to replace "+" to space. For example when in field "opraCode" we get value "IBZ+GJ" it should be mapped to field symbol as "IBZ GJ". After should time I decided to write a class that can do those calculations. It can do as all arithmetic calculations as all calculations I described above. It can operate over strings

( abc+dfg = abcfdg ), numeric values ( 2+2/2=3 or 3+4*2/(1-5)^2 = 3.5 ).

If you want to replace strings which contains some math operation symbols like "+", "-", "*", "/" etc. you should use apostrophe like : replace('+',' ','IBZ+GJ').

From my point of view - the code below is a good example how to convert Infix notation to Postfix( Dijkstra ) and then calculate expression.


Please let me know if any errors occurred at mail javaguru@mail.ru

Maxim Sukhomud.

Download:

source in archive

source as java file

javadoc

Hosted by uCoz