jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Stacks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Stacks" Watch "Stacks" New topic
Author

Stacks

Arthur Zadrozny
Greenhorn

Joined: Sep 19, 2002
Posts: 3
Hi, I am writtig a program that evaluates postfix notations. I am using a StringTokenizer to step through individual tokens in the expression. It returns each token as a string but I need the tokens that are not operators to be parsed as integers and pused onto the stack. How do I do that? Here is some of my code:
static int evaluate(String str)
{
ObjectStack stack = new ObjectStack();
StringTokenizer token = new StringTokenizer(str);

if (checkOperator(token.nextToken()))
{

stack.push(token.nextToken());
}
)
static boolean checkOperator(String str)
{
if (str == "+")
return false;

if (str == "-")
return false;

if (str == "/")
return false;

if (str == "*")
return false;

return true;
}
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

The first thing you want to do it use a loop to step through the tokens. Something like:

And the second thing, I think you want to use "compareTo()" instead of "==".
[ October 30, 2002: Message edited by: William Barnes ]

Please ignore post, I have no idea what I am talking about.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stacks
 
Similar Threads
FileIO and mult. arrays
Why iam getting ClassCastException
Getting value from Vector
Regarding string tokenizer
Stack Problem