import java.util.*;
class ReversePolishNotation{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Vector<String> vec = new Vector<String>();
while(sc.hasNext())
vec.add(sc.next());
System.out.println(reversePolishNotation(vec));
}
static int reversePolishNotation(Vector<String> vec){
Stack<Integer> stack = new Stack<Integer>();
//String operators = "+-*/";
for(String i : vec){
if(i != "+" && i != "-" && i != "*" && i != "/"){
`
here this if statement is executing even if i = "+"
`
stack.push(Integer.parseInt(i));
}
else{
int x = stack.pop();
int y = stack.pop();
switch(i){
case "+" : stack.push(y + x); break;
case "-" : stack.push(y - x); break;
case "*" : stack.push(y * x); break;
case "/" : stack.push(y / x); break;
}
}
}
return stack.pop();
}
}
this is my code for reverse polish notation evaluation, there is something wrong in "if" condition , this condition is ok in C++ and not in java
here is the error coming and i dont know this is ok in c++ but not in java INPUT : 2 1 + 3 *
Exception in thread "main" java.lang.NumberFormatException: For input string: "+" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:642) at java.base/java.lang.Integer.parseInt(Integer.java:770) at ReversePolishNotation.reversePolishNotation(ReversePolishNotation.java:21) at ReversePolishNotation.main(ReversePolishNotation.java:10)