Pushing to a stack question

C

Calvin

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
 
S

Sudsy

Calvin wrote:
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!
;-)
 
L

Lee Fesperman

Sudsy said:
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.
 
C

Calvin

Sudsy said:
Calvin wrote:


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
 
S

Sudsy

Calvin wrote:
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();
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top