Pushing to a stack question

Discussion in 'Java' started by Calvin, Apr 10, 2004.

  1. Calvin

    Calvin Guest

    Hello.

    import java.util.Stack;
    private Stack myStack; // insance var

    public void evaluate(String pExp)
    {
    myStack = new Stack();
    char ch = pExp.charAt(0);
    myStack.push(ch);
    // myStack.push((char)pExp.charAt(0));
    String result = (String)myStack.pop();
    resultArea.append("Stack pop is : "+result);
    }

    At this point, I want to push a char and pop a char so I can continue
    with my task but I can't simly push and pop a char. I know a stack
    holds objects and when I did get a string pushed my pop worked, but
    why wont eiter of my pushes work above (with char)?

    I get:
    The method push(Object) in the type Stack is not applicable for the
    arguments (char)

    I know this is simple, but a stack newbie I am. ( I have to use jav;s
    stack clas object)

    thanks
    calvin
     
    Calvin, Apr 10, 2004
    #1
    1. Advertisements

  2. Calvin

    Sudsy Guest

    Calvin wrote:
    <snip>
    > I get:
    > The method push(Object) in the type Stack is not applicable for the
    > arguments (char)
    >
    > I know this is simple, but a stack newbie I am. ( I have to use jav;s
    > stack clas object)
    >
    > thanks
    > calvin


    It's because char is a primitive, not a Class which inherits from
    java.lang.Object (the "root", if you will). Check out the
    java.lang.Character class. It has a constructor which takes a
    primitive char as an argument.
    So maybe you end up with something like this:
    myStack.push( new Character( ch ) );
    Now you have an Object which agrees with the method signature for
    the push method. And when you "pop", you use something like this:
    char result = ( (Character) myStack.pop() ).charValue();
    Excuse the extraneous brackets, but it makes the intent clear.
    Recent discussions regarding test questions with "gnarly" operator
    precedence connotations suggest that you can never be too careful!
    ;-)
     
    Sudsy, Apr 10, 2004
    #2
    1. Advertisements

  3. Sudsy wrote:
    >
    > char result = ( (Character) myStack.pop() ).charValue();
    > Excuse the extraneous brackets, but it makes the intent clear.
    > Recent discussions regarding test questions with "gnarly" operator
    > precedence connotations suggest that you can never be too careful!
    > ;-)


    Actually, none are extraneous; you need all of the above (round) brackets to get the job
    done ... it won't compile without them.

    --
    Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
     
    Lee Fesperman, Apr 10, 2004
    #3
  4. Calvin

    Roedy Green Guest

    On 9 Apr 2004 22:44:31 -0700, (Calvin) wrote or
    quoted :

    >push a char and pop a char s


    You can only push and pop objects e.g. Characters. See
    http://mindprod.com/converter.html for how to convert char <->
    Character.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 10, 2004
    #4
  5. Calvin

    Calvin Guest

    Sudsy <> wrote in message news:<>...
    > Calvin wrote:
    > <snip>
    > > I get:
    > > The method push(Object) in the type Stack is not applicable for the
    > > arguments (char)
    > >
    > > I know this is simple, but a stack newbie I am. ( I have to use jav;s
    > > stack clas object)
    > >
    > > thanks
    > > calvin

    >
    > It's because char is a primitive, not a Class which inherits from
    > java.lang.Object (the "root", if you will). Check out the
    > java.lang.Character class. It has a constructor which takes a
    > primitive char as an argument.
    > So maybe you end up with something like this:
    > myStack.push( new Character( ch ) );
    > Now you have an Object which agrees with the method signature for
    > the push method. And when you "pop", you use something like this:
    > char result = ( (Character) myStack.pop() ).charValue();
    > Excuse the extraneous brackets, but it makes the intent clear.
    > Recent discussions regarding test questions with "gnarly" operator
    > precedence connotations suggest that you can never be too careful!
    > ;-)



    Yes. I was able to somewhat figure that out after posting. However,
    whith that logic, the following should work and Im still getting the
    same errot.

    public void evaluate(String pExp)
    {
    myStack = new Stack();
    StringTokenizer tokens = new StringTokenizer(pExp, "()+-*/ ",
    true);
    String token;
    while (tokens.hasMoreTokens())
    {
    token = tokens.nextToken();
    System.out.println(token);
    myStack.push(new Character(token.charAt(0)));
    String result = (String) myStack.pop();
    resultArea.append("Stack pop is : " + result);
    }

    Calvin
     
    Calvin, Apr 10, 2004
    #5
  6. Calvin

    Sudsy Guest

    Calvin wrote:
    <snip>
    > myStack.push(new Character(token.charAt(0)));
    > String result = (String) myStack.pop();


    That will give you a class cast exception. You pushed a Character
    object and you're trying to cast it to String on the pop. Reread
    my previous post. You want something like this:
    char result = ( (Character) myStack.pop() ).charValue();
     
    Sudsy, Apr 10, 2004
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Macbell

    Pushing the limit of PHP vs ASP.NET

    James Macbell, May 14, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    865
    Nikolai Chuvakhin
    May 15, 2004
  2. =?Utf-8?B?ZVhzZXJhcGg=?=

    How can I implement the "Pushing" mode in Asp.Net application

    =?Utf-8?B?ZVhzZXJhcGg=?=, Nov 19, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    691
    bruce barker
    Nov 22, 2004
  3. Dave
    Replies:
    2
    Views:
    492
    Jim Langston
    Aug 15, 2007
  4. Surinder Singh
    Replies:
    1
    Views:
    1,465
    Richard Bos
    Dec 20, 2007
  5. Casey Hawthorne
    Replies:
    3
    Views:
    1,409
    Flash Gordon
    Nov 1, 2009
  6. Debajit Adhikary
    Replies:
    36
    Views:
    3,165
    Andre Kaufmann
    Feb 10, 2011
  7. Sam Roberts
    Replies:
    1
    Views:
    376
    Yukihiro Matsumoto
    Feb 11, 2005
  8. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    541
    Kenneth McDonald
    Sep 1, 2007
Loading...