DEV Community

Discussion on: Writing a mathematical expression evaluator in Java

ricobrase profile image
Rico Brase

Interesting approach!

I've personally done something similiar in a project a few weeks ago.
First, I'm taking the input in infix notation (1 + 2) and convert it into RPN (reverse polish notation, 1 2 +) using Dijkstra's Shunting-yard algorithm.

Having the expression in RPN format, you start solving it by pushing values and operators on a Stack and pop()-ing through them, until no more data to process is available.

You can see my code over here on Github inside the project I used it for:

Asscociated unit test:

There are some code leftovers from experimenting with Optionals, just ignore that. 😉