• 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

Stack Problem

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


 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This means that at line 52 you have cast that failed because you tryed to cast something (probably Integer) but it is a String.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought there was a way to cast the object that is being pushed onto the stack.

Eric
 
Jean-Sebastien Abella
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it helps, this is the original program that I am trying to use as a guide:

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

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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic