Java Stack Operations Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Smith

    Java Stack Operations Help

    New to Java here.
    I'm trying to pop the top two elements from a stack, add them together and push the result back onto the stack.

    So far I have:

    Code:
     
        Object a = stack.pop();
        Object b = stack.pop();
        int int1 = Integer.parseInt((String)a);
        int int2 = Integer.parseInt((String)b);
        stack.push(int1 + int2);
    It seems to not allow me push an int onto the stack and asks for a token.

    push(Token) in Stack <Token> cannot be applied to (int)
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    It is working fine if type is not declared to the Stack.

    Please post the whole code.

    Regards
    Dheeraj Joshi

    Comment

    Working...