aspose file tools*
The moose likes Beginning Java and the fly likes Stacks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Stacks

Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
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 ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

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())


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12

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 ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
Inconvertible types is the exact message.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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?


"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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

Joined: May 26, 2005
Posts: 43
This is the ArrayStack file:

[ November 26, 2005: Message edited by: Jim Yingst ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
That's what I thought. So how do I get my sentence string to print in reverse?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: May 26, 2005
Posts: 43
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: May 26, 2005
Posts: 43
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: May 26, 2005
Posts: 43
Ok, when I type in my sentence "Josie o mine", I get "Josie o minemine o Josie"

Here's my code:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Continued here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stacks