• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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
0
pete420
Asked:
pete420
  • 10
  • 6
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now