java - Error incompatible types. Postfix evaluation -
my task create program postfix evaluation using array , char. i'm having trouble problem
incompatible type: object cannot converted int.
here's code:
import java.util.*; public class stackpostfixeva { //class name public static void main(string args[]) { scanner key = new scanner(system.in); //initialize scanner char[] postfix = new char[10]; //creating array system.out.println("please enter postfix expression. enter '#' if have finish entering postfix expression "); //instruction command int i; //initialize variable (i = 0; <= postfix.length; i++) { //loop receiving input postfix[i] = key.next().charat(i); //input command if (postfix[i] == '#') { //to indicate end break; } } system.out.println("the postfix expression are:"); //to print postfix expression (i = 0; <= postfix.length; i++) { system.out.println(postfix[i]); } stack st = new stack(); //creating stack int result, ch1, ch2; //initialize variable (i = 0; <= postfix.length; i++) { //loop scanning each char if (postfix[i] >= '0' && postfix[i] <= '9') { //to determine operand st.push((int) postfix[i] - '0'); //push operand } else { //execution if operator found ch1 = st.pop(); //problem here ch2 = st.pop(); //problem here switch (postfix[1]) { case '+': result = ch2 + ch1; break; case '-': result = ch2 - ch1; break; case '*': result = ch2 * ch1; break; case '/': result = ch2 / ch1; break; case '%': result = ch2 / ch1; break; default: result = 0; } //end switch st.push(result); } //end else } //end result = st.pop(); //problem here system.out.println(result); } }
you using stack store integer
values, i'd suggest specifying generic type:
stack<integer> st = new stack<>();
that way st.pop()
have type integer
, autoboxed int
.
when declare stack
(with no type parameters), pop()
returns object
not convertible int
without explicit cast (offered in answer).
Comments
Post a Comment