aspose file tools*
The moose likes Beginning Java and the fly likes Stack Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stack Problem" Watch "Stack Problem" New topic
Author

Stack Problem

eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Hello,

I am having some problems understanding the use of Objects in a stack.
I am getting errors for these lines:
//stack.push( st.nextToken() );
//y = stack.pop();
//x = stack.pop();

Should I be converting to an Integer here?
Thanks,
Eric


Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
Hi Eric,
Yes you need to Cast it before you can assign it to your Integer object.
the function pop of stack return an object.

The reason for that is that you can put anything in the stack so it can only assume it will be an Object.

so in the code it should looks like that:
x = (Integer) stack.pop();


It is the same for ArrayList or Vectors when you call the function get, actually any object of the Collection API.
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Hello Jean,

Thank you for the help.

I have written it to cast as you suggested, but now I am getting this:


Thanks,
Eric
[ August 11, 2005: Message edited by: eric elysia ]
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
This means that at line 52 you have cast that failed because you tryed to cast something (probably Integer) but it is a String.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Layne


Java API Documentation
The Java Tutorial
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
I thought there was a way to cast the object that is being pushed onto the stack.

Eric
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
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.
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
OK, I have revised my program. I feel like I am getting in over my head.

I hope I am on the right track here. I am now working on displaying the output.

Your comments are welcomed.

Thanks,
Eric
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by eric elysia:
I thought there was a way to cast the object that is being pushed onto the stack.

Eric


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.

Layne
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Layne,

The new code does compile and run.

My testCaseOutput is displaying correctly.

I do not have output displaying anything yet.

I am not sure whether the evaluatePostfixExpression method is correct.

I am in the process of walking back through my code to make sure it is doing what I want it to do.

I would like to test the stack first to make sure the string buffer can be read into the stack.

I also want to add exceptions to this program.

Thank you,
Eric
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
After looking over the code, here are some things I need help with.

If I input anything other than a number, I get a NumberFormatException.

Method displayCharArray() is not able to be invoked.

Method printStack() calls method print() in the List class. I need to be able to set this as output.

I'm not sure if I need method createStack(). I just wrote it to see if I might be able to use it.

Eric
[ August 12, 2005: Message edited by: eric elysia ]
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
I am getting the NumberFormatException because I call method getToken(), and token is an int in class Token.

I'm not sure what I need to change to allow operators to be input as well as numbers.

Thanks,
Eric
[ August 13, 2005: Message edited by: eric elysia ]
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
My main questions are:

If I input anything other than a number, I get a NumberFormatException. I realize that this is happening because the method getToken() returns an int. How do I allow operators to be input?

Method displayCharArray() is not able to be invoked. I have tried this also with an array as a parameter, but it is still not being invoked. Any suggestions?

Method printStack() calls method print() in the List class. This is printing the stack to the Run I/O of the compiler, but I need to be able to set this as output. I am not sure how to do this.

Thanks,
Eric
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Layne
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Hello Layne,

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 ]
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
If it helps, this is the original program that I am trying to use as a guide:

Eric
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Layne,

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.

Eric


[ August 14, 2005: Message edited by: eric elysia ]
[ August 14, 2005: Message edited by: eric elysia ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by eric elysia:
...int token, which is used to represent the token by number...


So what does this number MEAN? In other words, what are the possible legal values that can be asigned to this token field? (Hint: take a close look at the source code for this Token class.)

Layne
[ August 14, 2005: Message edited by: Layne Lund ]
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
In the Token class, the int is used to store one of the declared constants from the PostfixEvaluator class, (PLUS, SUB, TIMES, DIV, or NUMBER).

I am no longer using the Token class, but I can use it if you want to continue discussing it.

Thank you,
Eric
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Layne,

Thank you for your interest in my program. Unfortunately, I have to turn the program in now. Below is the final program that I am submitting.

Eric
 
wood burning stoves
 
subject: Stack Problem