This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# Trouble evaluating postfix Expression

Christopher Beech
Ranch Hand
Posts: 40
Trying to write a program that evaluates postfix Expressions. Doesn't give me the correct answer. For example, if I enter 234+*, it gives me -6.0 instead of 14.

Keith Lynn
Ranch Hand
Posts: 2409
One potential problem I see is with this line.

Won't this always be true?

Christopher Beech
Ranch Hand
Posts: 40
I think so, but we are meant to assume that the expression consists of only single-digit operands.

Keith Lynn
Ranch Hand
Posts: 2409
The problem is that since that is always true, the else doesn't execute.

Christopher Beech
Ranch Hand
Posts: 40
How can that always be true??? Wouldn't + and * make that if statement false???

Keith Lynn
Ranch Hand
Posts: 2409
The statement reads if the value of ch is greater than -10 or less than 10. All values of ch are greater than -10 or less than 10.

Did you mean to have an and?
[ April 18, 2006: Message edited by: Keith Lynn ]

Christopher Beech
Ranch Hand
Posts: 40
Ok. How do I make it so that it checks that the int is greater than -10 or less than 10??? Would that fix the problem???

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
Actually NO values of ch are greater than -10 and less than 10. When you read a '0' from the console, the value as an integer is 48 -- the Unicode value of the character '0'. Likewise, '1' is 49, '2' is 50, etc. The odd thing here is that you seem to realize that, as you're subtracting '0' from ch before using the numeric value.

The idiomatic Java way to do this would be to use the static methods Character.isDigit() to test if a character is a digit, and Character.digit() to convert from a character to a number. Then your program works internationally, not just for ASCII-using locations like the US.

Keith Lynn
Ranch Hand
Posts: 2409
So basically the if statement is trying to place numbers in the stack. Look at the values of the digits as ints and that will help you with the if.

Christopher Beech
Ranch Hand
Posts: 40
I tried replacing the if statement with

if (ch.digit(ch) > -10 ||ch.digit(ch) < 10)

and with

if (ch.digit > -10 ||ch.digit < 10)

but each time it said that char cannot be dereference.

Keith Lynn
Ranch Hand
Posts: 2409
You can't call a method on a primitive. What he meant in his suggestion was to use the Character.isDigit method and send the char to it.

Christopher Beech
Ranch Hand
Posts: 40
You mean like

if(Character.isDigit(ch) > -10 || Character.isDigit(ch) < 10)

That's not right, either. It doesn't like the > and < operators now.

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
No, just

if (Character.isDigit(ch))

Christopher Beech
Ranch Hand
Posts: 40
Would this work???

Should have put that through the complier first. That gives me a cannot find symbol error.
[ April 18, 2006: Message edited by: Christopher Beech ]

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
No, just something like

Christopher Beech
Ranch Hand
Posts: 40
Now I'm getting an Empty stack exception.

(Chris goes to read the page.)

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
This

else if(ch == t.pop())

is asking whether the character just read is the same as the one on the stack. Since the only ones you ever push on the stack are numbers, this will be true if, and only if, you just read a number. You'll push it on, and then immediately pop it off.

I can't really tell what you intend to happen here.

Christopher Beech
Ranch Hand
Posts: 40
The purpose of that line is to check if the ch is an operator.

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
Originally posted by Christopher Beech:
The purpose of that line is to check if the ch is an operator.

OK, can you explain why you think that's what it does? If I understood that, it'd be easier to help you decide the right way to do it.

Christopher Beech
Ranch Hand
Posts: 40
I think I just realized my mistake. A quick question to make sure.

This code should handle all cases where ch is a number, right??? Assumimg that is correct, if ch gets past the above code, it had to be an operator.

Am I on the right track here???

This would mean I need to put something else in the () for the else if statement.
[ April 18, 2006: Message edited by: Christopher Beech ]

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
Your statements are correct. I would say you need an isOperator() function, which you can call to confirm that a character is an operator. Then you can write

The isOperator method is probably just like

Or the equivalent using "if (ch == '+' || ch == '-' || ..."