I am creating a program that is basically a calculator, which takes in input from the user and breaks it into different parts. Example input:
4 + 5
My program takes that input and analyzes it and breaks down the terms, the operators, omits white space, and some other things. However, when I put all the pieces back together, they are in a String form. So it looks like this:
"4+5"
How can I have the program take that string and parse it to yield the int 9?
I tried Integer.parseInt(input)
, and to no surprise, it returns an error. So is there a simple (enough) way to do this? Or will I have to create my own method to parse the equation?
By the way, it can do more than two terms, and the reason I break down the input is to check for input errors, such as 5 ++ 5 or anything of that manner. It is also to incorporate PEMDAS.