Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stacks

 
Arthur Zadrozny
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic