File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Computing a string 'calculations' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Computing a string Watch "Computing a string New topic
Author

Computing a string 'calculations'

Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Hi guys,

I was trying to comopute a string, such as "32*45-7", I'm unsure how to go about this.
Do I need to break it down and compute each part...? I tried
but is throws an error.

I think my main problem is preserving the operand..? and its position, alot of the calculations have '((' etc..

Any thoughts on how I can tackle something like thingsRegards
Zein
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

You have to parse the expression keeping in mind operator precedence.
Here is a programming assignment that gives a pseudocode algorithm along with a discussion.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Hi there,

Thanks for your reply.

I was just wondering, given this is (from my perspective) a very common opeation, is there an libraries that Java provides to evaluate arithmetic expressions.

My calculations at the most will have +/-* and brackets... i.e. - (((5+65)/5) + (16+26/2)? Any ideas..?

Thanks
Zein
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Not to my knowledge. Certainly not in the Java SE/EE APIs.
A quick Google search for "infix calculator" returns lots of academic programming assignments. Maybe you could find a library in those results.
[ November 02, 2007: Message edited by: Joe Ess ]
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
This recent thread seems to be about the same problem.

JEP is a commercial library doing this, but that might be a little overkill.
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Hi guys,

Thank you for your responses.

I searched far and wide for an answer and in the end setteled with JEP. See this link http://www.singsurf.org/djep/html/index.html

It does the job perfetley!!

Regards
Zein
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
I am currently adding such capabilities to an open source project of mine. I am using antlr to parse the expressions. The learning curve for antlr is a little steep (There is a book by the creator Terrence Parr that is good though), though pretty easy to work with once you get the hang of it.

It would be nice if this was added as part of the java platform.

Here is a link to the antlr grammar that i use to do the math. http://fdsapi.cvs.sourceforge.net/fdsapi/fdsapi/Code/com/fdsapi/parser/FDSMath.g?revision=1.3&view=markup

Here is some usage.





This code is not part of the current release, so if you download the jar it will not work.
[ November 08, 2007: Message edited by: steve souza ]

http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
If I may toot my own horn, the just-published edition of the JavaRanch Journal contains an article by me about how to create real Java classes from user-defined functions at runtime. That's quite a bit faster than using a library like JEP that basically interprets the function.

There are other libraries that come much closer in speed to compiled code, like JEL.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Computing a string 'calculations'