• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Casting to and from Stacks

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got a few questions regarding Stacks and casting to and from them.
I've been trying to use stacks to do calulations from strings such as 43+ and 84/2/53+6*- and in the process a few questions about Stacks and casting have popped up.
I've been taking the String and extracting each digit and placing it in the Stack as a Character until I reach an operator. However I noticed that when I popped the items from the Stack as Integer and then to int to do the calculation I received odd values. I've deduced that I'm getting the ASCII value of the Character returned.
So one question I have is how does a Stack deal with the Object it receives? I was under the false impression that the number character from the String I was extracting and placing into the Stack as a Character would return as that number when I cast to an Integer on the way out. Now a Boolean I think I can figure out, however what is the difference between a String of "1" that I place into a Stack as String, vs. a Character vs. an Integer?
A second question is in regards to the operator. How should I deal with a +,-,*,/? Should that be taken from the String and cast down to a char in order to do the calculation once the operands are cast down to ints? In addition, are their any nuances regardings the "+" in this situation possibly concatanating as opposed to adding?
Regards, Michael
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most programming tyros run into this problem sooner or later, but it has nothing to do with stacks. Characters representing numerical digits have ASCII values just like any other character, but it's not the same as the value of the digit. For example, the character '1' has ASCII value 49 (if I remember right) and that's what you're going to get if you cast it to an integer. What you probably want to do is use Integer.valueOf() or Double.valueOf() on a series of digits, and push the result of that onto the stack. That will be a number that you can use.
As far as the operator, you can push it onto the stack as a character. Your evaluation mechanism will have to look at that character and decide (using if-then-else or switch) what arithmetic to perform.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So one question I have is how does a Stack deal with the Object it receives? I was under the false impression that the number character from the String I was extracting and placing into the Stack as a Character would return as that number when I cast to an Integer on the way out. Now a Boolean I think I can figure out, however what is the difference between a String of "1" that I place into a Stack as String, vs. a Character vs. an Integer?


A Stack is just a Vector with push, pop, etc. added behavior. Vectors, like all Collections, just maintain a collection (list, set) of object references. A Character, as you have surmized, cast to an int, (int) Character.charValue(), returns the unicode value of the Character. The state of a String is an immutable group of unicode characters, a Character's state is a single immutable unicode character and an Integer's state an immutable 32 bit integer.


A second question is in regards to the operator. How should I deal with a +,-,*,/? Should that be taken from the String and cast down to a char in order to do the calculation once the operands are cast down to ints? In addition, are their any nuances regardings the "+" in this situation possibly concatanating as opposed to adding?


I think I would use Integer for the digits, utilizing the Integer(String) constructor just before pushing onto the stack. That way, instead of pushing 1, 2, 3, you could push new Integer("123"). You can use either String or Charactor for the operators. Since you are presumably doing the math there should be no ambiguity in the meaning of the operators.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would also consider using Integer.parseInt(string) method so that you would get the int representing the value that you are looking for. If you use the Integer(string) constructor, then you have to call Integer.intValue() to get the integer value back for your mathematical operations.
This is just a thought.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If you use the Integer(string) constructor, then you have to call Integer.intValue() to get the integer value back for your mathematical operations.


Not if you do as I suggested and push new Integer("123") onto the stack. When you pop, you'll have a wrapped value of 123. If you use Integer.parseInt("123"), you'll have to push new Integer(Integer.parseInt("123")). Why go to all that trouble?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic