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

Stacks

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have a program that is supposed to reverse a string of words. The code is below. I get an error code for "word = (String)stack.pop();" It says inconvertible types.

What does that mean?

[ November 26, 2005: Message edited by: Jim Yingst ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
i don't remeber the exact precedence, but it's possible that java is casting your stack to a string, and then calling the pop() method - although it doesn't really get that far.

try something like

word = (String)(stack.pop())
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I changed your ArrayStack to Stack (ArrayStack must be something you made?) and compiled with 1.5 and did not see any errors. Can you copy the exact message in?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

And I compiled and ran with jdk1.4 where ArrayStack extends Stack with no problem. Can you copy the error from your output rather than just trying to summarize it?
[ November 25, 2005: Message edited by: Marilyn de Queiroz ]
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Inconvertible types is the exact message.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Exactly what is this "ArrayStack" you're using? Stan and Marilyn tried assuming it's some sort of java.util.Stack, and their code works. If yours is not working, I suspect this "ArrayStack" is something other than a java.util.Stack. In particular, I think that the pop() method returns something other than a String. Which means it can't be cast to a String. What does pop() return?
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This one? http://struts.apache.org/struts-doc-1.0.2/api/org/apache/struts/util/ArrayStack.html

pop returns Object, ought to be fine, no?
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This is the ArrayStack file:

[ November 26, 2005: Message edited by: Jim Yingst ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
There you go. Your pop() returns void. You can't cast void to a String (or to anything else!)

This stack implements a "two-phase pop:" you call top() to get the top element, then pop() to discard it and bring the next one to the top.
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
That's what I thought. So how do I get my sentence string to print in reverse?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Um... by folloing EFH's last suggestion? I'm not sure what "that's what I thought" means here. Did you already knw this? Does the code compile now? I just did what EFH said and the code worked fine - input of

one
two
three

became

three
two
one

Isn't that what you want it to do?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Also, I added [code] tags to your previous posts. Please use these in the future whenever you post code, to preserve the original indentation. That makes it more readable for everyone. Thanks.
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Right I figured that out...the top then the pop. I just can't figure out the correct pop syntax. This is what I've tried.

System.out.println("The words in reverse order are:");
while (!stack.isEmpty()) {
word = (String) stack.top();
System.out.println(word);
stack.pop();

and

System.out.println("The words in reverse order are:");
while (!stack.isEmpty()) {
word = (String) stack.top();
stack.pop();
System.out.println(word);

Both ways, It spits out the original sentence. I've tried other things too, but then I either get an error about inconvertible types or when I hit return, it spits out the sentence over and over and over again.

Please help.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hey, look! More code! If only there were some way to get that code to display with proper indentation. Hmmm....

For the correct way to use top() & pop(), I don't think it matters. Either one above is fine. As it happens I used the first one in my code.

When you say "sentence", are you putting all these words on one line? (Like a normal sentence?) Did you notice the instructions say "Enter a sequence of strings one per line..."?
[ November 26, 2005: Message edited by: Jim Yingst ]
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Sorry, I didn't read that part of your post until after I posted. I'll try to remember next time.

Yes, I noticed. The instructions are to input a sentence, like "My dog has fleas" and it's supposed to return "fleas has dog My." Obviously there's something wrong with the other stuff. I just tried it one word at a time and it reversed, but it needs to be in a sentence.

More help please.

[ November 26, 2005: Message edited by: Stephanie Dears ]
[ November 26, 2005: Message edited by: Stephanie Dears ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
OK, so if the program is reversing lines correctly, then the top() and pop() are working correctly. There's no way you can reverse words within a line just by modifying the top() and pop(). The problem is that earlier, you read lines with readLine(), and put the whole line onto the stack at once. I think after you read a line, you will need to split it up somehow into separate words, then push each word onto the stack, separately. There are several classes you could use for this: StringTokenizer, the split() method in String or the Scanner class in JDK 5. To use the last two you would need to know a bit about regular expressions; if you don't know abou these yet you should probably look at StringTokenizer first.
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Ok, when I type in my sentence "Josie o mine", I get "Josie o minemine o Josie"

Here's my code:
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Continued here.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic