You are storing the string "22+"
in String[] str = {"22+"};
and then you are trying to use it as number in a = Integer.parseInt(stack.peek());
So, if you want to use that notation, you will have to parse your stack.peek()
result.
If you know that peek()
will return 22+
so you can do:
stack.peek().split("\\+")[0]
Btw, the java.lang.NumberFormatException: For input string: "[22+]"
is exactly that, you are trying to convert a number using the invalid string 22+
for an integer. The line which return this error is: Integer.parseInt(stack.peek());
A debugger can help you to find this issue and you can see it easily by dividing:
a = Integer.parseInt(stack.peek());
Into:
String elem = stack.peek();
a = Integer.parseInt(elem);