File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Stacks" Watch "Stacks" New topic


Arthur Zadrozny

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()))

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: 986

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:
subject: Stacks
It's not a secret anymore!