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
pete420Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
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 *") );
   }
0
zzynxSoftware engineerCommented:
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();
   }
0
objectsCommented:
Code looks fine, check that you have compiled all the classes.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

pete420Author Commented:
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

:)

0
zzynxSoftware engineerCommented:
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.
0
pete420Author Commented:
expression = 20 - Result: 0


thanks for reply
0
zzynxSoftware engineerCommented:
That's what I expected. :)
Looks like your ReadString() uses spaces as delimiters.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pete420Author Commented:
ahhh ok,


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

thanks again

pete
0
zzynxSoftware engineerCommented:
You're welcome :)
0
pete420Author Commented:
u were spot on,

much appreciated


pete
0
zzynxSoftware engineerCommented:
Thanks for accepting
0
zzynxSoftware engineerCommented:
PS. Any reason why I got a B-grade?
0
pete420Author Commented:
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.
0
zzynxSoftware engineerCommented:
I'll ask a mod to reopen this Q.
Afterwards you can just reaccept.
0
pete420Author Commented:
ok sounds fair
0
zzynxSoftware engineerCommented:
>> sorry i didnt mean any offence by it,
No worries, I didn't interprete is a such, you know ;°)
0
zzynxSoftware engineerCommented:
>>Grade changed to A.
Thanks

>> For grade changes there is no need to reopen the question
Right, I forgot that.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.