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
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
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();
}
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.
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(express ion));
}
}
objects: ta for reply but all classes are compiled
:)
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
}
}
objects: ta for reply but all classes are compiled
:)
Could you replace
>> System.out.println("Result : " +evalTest.evaluate(express ion));
by
System.out.println("expres sion=" + expression + " - Result: " +evalTest.evaluate(express ion));
and post here what that gives.
>> System.out.println("Result
by
System.out.println("expres
and post here what that gives.
ASKER
expression = 20 - Result: 0
thanks for reply
thanks for reply
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ahhh ok,
i will try a Buffered Reader instead and let u know :)
thanks again
pete
i will try a Buffered Reader instead and let u know :)
thanks again
pete
You're welcome :)
ASKER
u were spot on,
much appreciated
pete
much appreciated
pete
Thanks for accepting
PS. Any reason why I got a B-grade?
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.
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.
Afterwards you can just reaccept.
ASKER
ok sounds fair
>> sorry i didnt mean any offence by it,
No worries, I didn't interprete is a such, you know ;°)
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.
Thanks
>> For grade changes there is no need to reopen the question
Right, I forgot that.
public static void main (String args[]) {
PostfixEvaluator pe = new PostfixEvaluator();
System.out.println( pe.evaluate("20 5 - 3 *") );
}