At this point, when I evaluate 12+, it gives me 99. I know that's partly due to the decimal value of the char, but I'm really stuck on how to do this.
Hm, no thoughts at all?
I agree about the way it's wrritten, I am afraid.
If I remember correctly, that is an exercise from the Dragon Book, which specifically uses one-digit numbers. Pushing the int 16 back onto the stack will allow you to pop 16 later, still as an int.
Just a question: Does the assignment includes writing a stack or is it ok to use the Java Stack implementation (http://download.oracle.com/javase/6/docs/api/java/util/Stack.html)??
Those 48 and 57 magic numbers are error-prone.
Campbell Ritchie wrote:
5: Stack now 16 5 - Pop the 5 and 16, subtract the two and push the result, so the stack is now 11.
Campbell Ritchie wrote:Only noticed you are new today. Welcome
Sounds fine to me; except I think I would call it 'input' .
Josh Theisen wrote:Am I even on the right track to go through the output character by character?
No sweat. We all started in the same place.
Forgive me, I'm definitely new to this kind of stuff.
From what I understand I should take the number (if I get one, as opposed to getting an operator), which is a char, convert it to an int and then push it into the stack?
But you've got it now, yes?
I think the whole thing between chars and ints and how to convert them is my biggest problem.
Thanks again, guys.
Winston Gutkowski wrote:But you've got it now, yes?
All Campbell was saying is that it's better to use ch - '0' than ch - 48 for your conversion and ch >= '0' && ch <= '9' for your range check (more portable).
Erm, no. Each case in a switch statement is a single value (except for default:), so they're not very good for ranges.
Josh Theisen wrote:I guess the only question I have now is how would I incorporate that range check into a switch statement? ...
Josh Theisen wrote:I definitely like this way better as of now, sure beats the old one. Any thoughts again?
Josh Theisen wrote:EDIT: Winston, for your last couple of suggestions - is it possible for me to put the intChar inside the if loop? I was messing around with that, and then it hit me - the intChar isn't initialized yet so I can't compare whether or not it's >= 0 or <=9.
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton