The program is about asking the user to write an expression and then pushing the numbers into a stack. The data type of the expression must be string. I have started with the code, but the problem is that the program sees every digit as a seperate one. For example if I want the stack to push the number 30 it will push 3 then push 0. Is there any simple ways solving the problem?
enter code here
public class Main {
public static void main(String[] args) {
String userInput = " (14 + 2 * 33)";
System.out.println(evaluate(userInput));
}
public static int evaluate(String expression)
{
char[] tokens = expression.toCharArray();
Stack<Integer> values = new
Stack<Integer>();
for (int i = 0; i < tokens.length; i++)
{
if (Character.isDigit(tokens[i])) {
values.push(Integer.parseInt(String.valueOf(tokens[i])));
}