Link to home
Start Free TrialLog in
Avatar of pete420
pete420

asked on

Post Fix evaluator

hi,

source code @ http://www.newwavesound.co.uk/java

my problem is that when the usePostfixEvaluator program is run and i enter the expression

20 5 - 3 *

the answer is returned as 0, whearas the correct answer should be 45



i know my code is not far off the answer and its probaly something im overlooking.

Help is much appreciated


pete
Avatar of zzynx
zzynx
Flag of Belgium image

I don't know how you tested it, but this prints out 45 as expected:

   public static void main (String args[]) {
      PostfixEvaluator pe = new PostfixEvaluator();
      System.out.println( pe.evaluate("20 5 - 3 *") );
   }
Well, I must say that I changed StackADT to Stack (to get it compiled):

   import java.util.Stack;   // << added

   //private StackADT stack;   // replaced by:
   private Stack stack;

   //-----------------------------------------------------------------
   //  Sets up this evalutor by creating a new stack.
   //-----------------------------------------------------------------
   public PostfixEvaluator()
   {
       // stack = new LinkedStack();   // replaced by:
       stack = new Stack();
   }
Code looks fine, check that you have compiled all the classes.
Avatar of pete420
pete420

ASKER

zzynx:
when i use your code   System.out.println( pe.evaluate("20 5 - 3 *") );

i get the correct answer same as you, however, i want the expression to be inputted by the user so when i go back to the way my code was i keep gettin 0 outputted. I didnt even have to change StackADT as mine compiles fine.

public class UsePostFixEvaluator
{
      public static void main (String args[])
      {
            PostfixEvaluator evalTest = new PostfixEvaluator();
            //receives expression from user
            System.out.println("Enter expression");
            String expression = uuInOut.ReadString();
            

      System.out.println("Result: " +evalTest.evaluate(expression));
      }
}


objects: ta for reply but all classes are compiled

:)

Could you replace
>> System.out.println("Result: " +evalTest.evaluate(expression));
by
System.out.println("expression=" + expression + " - Result: " +evalTest.evaluate(expression));

and post here what that gives.
Avatar of pete420

ASKER

expression = 20 - Result: 0


thanks for reply
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pete420

ASKER

ahhh ok,


i will try a Buffered Reader instead and let u know :)

thanks again

pete
You're welcome :)
Avatar of pete420

ASKER

u were spot on,

much appreciated


pete
Thanks for accepting
PS. Any reason why I got a B-grade?
Avatar of pete420

ASKER

sorry i didnt mean any offence by it,

if i can change it i will,
i just thot the top grade would be used more sparingly but i see what u mean, y say just good when u just got me out of a situation i was stumped at.

i will see if i can change it.
I'll ask a mod to reopen this Q.
Afterwards you can just reaccept.
Avatar of pete420

ASKER

ok sounds fair
>> sorry i didnt mean any offence by it,
No worries, I didn't interprete is a such, you know ;°)
>>Grade changed to A.
Thanks

>> For grade changes there is no need to reopen the question
Right, I forgot that.