• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble evaluating postfix Expression

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One potential problem I see is with this line.



Won't this always be true?
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think so, but we are meant to assume that the expression consists of only single-digit operands.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that since that is always true, the else doesn't execute.
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can that always be true??? Wouldn't + and * make that if statement false???
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, just

if (Character.isDigit(ch))
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, just something like



Go read the Javadocs for these two methods so you understand them, OK? On this page.
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I'm getting an Empty stack exception.



(Chris goes to read the page.)
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of that line is to check if the ch is an operator.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 == '-' || ..."
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic