Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help I'm stuck on a reverse polish calculation

Thread Tools

Help I'm stuck on a reverse polish calculation

Posts: n/a
have been stuck on this for several days now.
I am trying to create a reverse polish calculator and I'm stuck at an
intermediate stage.
This is what I know I have to do (just not sure how to do it to much of a
newbie )
I'm trying to call method, isOp, in the int method, evaluate. While in INT
evaluate method and in a while(hasNextToken) loop. Calling isOp on each
token, nextToken.

If it returns False, convert to (Integer) and push the token into a Stack.
True, you will pop the two tokens, convert them to (Integer) and store them
into x and y respectively. Do the Arithmetic operation and then push it back
to the stack.

I'm pretty sure the IsOp test is good just can't figure out the first part.

I am trying to get the following code to work:
public class Evaluator {//tokList contains a list of tokens in postfix
//evaluate the expression and return the result
int x, y,resu;
int i,j=0;
public int evaluate (ArrayList tokList) {
while (tokList.hasNextToken()){
isOp (s);

Stack eval =new Stack();

if (false)
eval.push(new Integer(s));
{ y=((Integer) eval.pop()).intValue();
x=((Integer) eval.pop()).intValue();
switch (s)
case '+':
case '-':
case '*':
case '/':
eval.push(new Integer(resu));

resu= ((Integer) eval.pop()).intValue();
return resu;

// Check whether s represents an operator
private boolean isOp(String s) {
return( s.equals( " + " ) || s.equals( " -- " ) || s.equals( " / " ) ||
s.equals( " * " ) );

} //isOp

private boolean hprecedence(String a, String b) {
return !a.equals("(") && (b.equals("+") || b.equals("-")
|| a.equals("*") || a.equals("/"));
} //precedence



Any help would be greatly appreciated, I'm told it's quite simple once you
know how but I don't know....

Reply With Quote
Stewart Gordon
Posts: n/a
ron wrote:

> I am trying to get the following code to work:


Try getting it to compile first.

Also, please indent properly if you'd like us to be able to read the
code in order to help you. This will also greatly help you to find the
obvious mistake.


My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Reply With Quote

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
Reverse Polish Ruby Ruby 0 10-20-2006 02:06 PM
Reverse Polish Notation calculator Java 2 02-06-2006 09:21 PM
OuterHTML - loosing polish fonts schapopa ASP .Net 0 01-24-2005 12:58 PM
Reverse polish Steve Lambert C Programming 11 12-30-2004 04:00 AM
Stacks Queues Reverse Reverse Polish dogbite C++ 4 10-10-2003 05:06 AM