You need a parser.
You can either follow the frugi tips, or use the java library, or you can implement your own.
A simple parser to implement and suitable for this problem is the Shunting Yard algorithm.
Pros using your own analyzer:
- Significantly more control over grammar and expressions.
- Good experience.
- Fun
Minuses:
- It takes more time to prepare (obviously) than a third-party library.
- May be a mistake.
I would recommend writing your own!
source share