Hi basically I need help java and my program is an calculator which must be able to calculate arbitrary expressions like for example (5.0+8.1)*(2.0). My program works fine it does basic things like calling upon classes etc it's just I don't know how to do the arbitrary expressions. My calculator so far does all the basic things such as addition/subtraction/multiplication and division, but it must do arbitrary expressions.

I don't have to do system.input it just a program that tests calculations.

Any suggestions on how I can link them up?

Edited by Gen.Asura: n/a

5 Years
Discussion Span
Last Post by Gen.Asura

I don't know how to do the arbitrary expressions

There is a lot of literature that discusses how to parse expressions. This is a topic some people spend weeks studying when learning to write compilers.
Perhaps you can get an algorithm from Google or Wikipedia.


If your expression contains only { (,),digits,*,+,/,- } then you can use the "Javascript" Engine. Your choice...

But I do recommend what others have said. Convert the expression to Postfix and then use a stack to evaluate the expression. That way it would be a fun and joyful experience doing this. Good luck!!


lol thanks guys it did help, it sure as hell wasn't fun trying it but when I got it done and working I've earned a nice german beer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.