Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String in Java to Integer [duplicate]

Is there any way to convert string like "10+(8*9)" in Java to integer (result)? I'm trying to do it by parsing strings to characters looping trought them and somehow trying to get the result but it's really complicated so I'm just wondering if there is any other easier way to do this.

like image 712
Samuel Avatar asked Nov 20 '25 13:11

Samuel


1 Answers

What you have here is an Infix notation

It is very difficult to interpret directly, and is usually converted to RPN or Postfix notation first.

The Shunting-yard algorithm written by Dijkstra does the conversion for you. You should obtain a result that can be represented as:

10 8 9 * +

Once you have this, you apply the postfix algorithm to solve the stack you have.

like image 100
njzk2 Avatar answered Nov 22 '25 04:11

njzk2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!