Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Pushing to a stack question

Reply
Thread Tools

Pushing to a stack question

 
 
Calvin
Guest
Posts: n/a
 
      04-10-2004
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
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      04-10-2004
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!


 
Reply With Quote
 
 
 
 
Lee Fesperman
Guest
Posts: n/a
 
      04-10-2004
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)
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-10-2004
On 9 Apr 2004 22:44:31 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
Calvin
Guest
Posts: n/a
 
      04-10-2004
Sudsy <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      04-10-2004
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();

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
Pushing the registers onto the stack (x64). Dave C++ 2 08-15-2007 06:20 AM
"stack level too deep"... because Threads keep their "starting" stack Sam Roberts Ruby 1 02-11-2005 04:25 AM



Advertisments