So what are you pushing onto the stack? Even if pop() is declared to return an Object, the Object is also the same type as what was pushed onto the stack originally. You are only allowed to cast the return value to the same type as what was originally pushed on or any superclass of this type.
Hi Eric, When you push it doesn't matter as long as the thing you push is an Object, this is because the function push take an Object in parameter. So what is an object? Everything except int,float,char,long,double,short and bool.
Since the Stack as no clue after the push of what type of Object is it will simply pop it back as an object. But it doesnt matter since you should know what you put in the stack.
Originally posted by eric elysia: I thought there was a way to cast the object that is being pushed onto the stack.
Yes there is and Jean-Sebastion showed you how to do it. The problem is that you have to cast it back to the same type as whatever you originally pused onto the stack. It looks like in your original code you are pushing Strings onto the stack and then trying to cast them to Integers. Since a String cannot be cast to an Integer you are (were?) getting the ClassCastException. I haven't looked closely at your newest bit of code so I don't know if you fixed this particular issue.
So how does the new code work? Does it compile? If so what happens when you run it? If not, what compiler errors do you get? Please provide us some information to work with. I will be happy to help if you can give me some idea of what part of the code I need to look at.
Nothing in the Token class shown will cause the NumberFormatException that you describe, especially not the getToken() method that returns an int. I think you need to step back a second and describe what this Token class represents. In particular, I see that it contains two fields: token and value. What are these for? Did you write this class yourself or was it given by your professor? In either case, you need to understand how it is intended to be used. At this point, I am only able to guess what the intention of this class is, so I would much rather you explain it. I'll be glad to help you from there.
The Token class is part of an example program that I found online. The List class and StackComposition class were given in my text and must be used to maintain the PostfixEvaluator class.
The Token class was created to to represent a Token object. The class uses two variables, int token, which is used to represent the token by number, and double value, which is the actual value of the token. The Token class has two constructors. The first constructor takes one int argument, which assigns the int argument to token. The second constructor takes two arguments, int and double, which assigns the int argument to token and the double argument to value. Method getToken returns the current value of token. Method getValue returns the current value of value.
Eric [ August 13, 2005: Message edited by: eric elysia ]
Before you reply. Please take a look at my revised code. I think I have made things a little simpler. I think this code will be much easier to work with. I need to try to get this program finished tonight. I am willing to learn as much as I can.
The right parenthesis is only used to tell the program when to stop evaluating. I am really trying to make sure I understand every line of my program, but I am still having these problems. I need some help on where to declare the stack, StringBuffer, and charArray. Is it better to get rid of the charArray and just use the StringBuffer? I am not able to output the results into the output field (or testCaseOutputField). I would like to include an exception that only allows valid input. I would also like to include an empty list exception.
[ August 14, 2005: Message edited by: eric elysia ] [ August 14, 2005: Message edited by: eric elysia ]