• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with the stack ADT  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I'm currently working on a texteditor and I have run into a bit of an issue.



This is what I have initialized Stack as.

I'm trying to get a character which is typed into the editor in this method.


I have tried many different ways to write this method
such as text.push().toString(); etc but they all return the same error "Cannot invoke toString() on the primitive type char"
What I'm confused about is, the method says I have to return char but the stack has been set as a string.
How do I return the result char as a string to the stack?
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kan Za wrote:I have tried many different ways to write this method
such as text.push().toString(); etc but they all return the same error "Cannot invoke toString() on the primitive type char"

Well I highly doubt that your example does; but that's by-the-by.

Think about it: What does text.push().toString() actually do?
Explain it to me, every step, IN DETAIL; and I suspect, in doing so, you'll realise what the problem is.

And if you still don't get it, break that statement up, and assign each interim return value to a variable.

Question 2: If you are getting individual characters, why is your Stack a Stack<String>?

Winston

PS: I just noticed that you talked about ADT's, and Android's Stack class is NOT the same as Java's. However, that doesn't invalidate anything I said.

But if you'd like me to move this to our Android forum, I'd be happy to (you might get answers quicker).
 
Kan Za
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it: What does text.push().toString() actually do?
Explain it to me, every step, IN DETAIL; and I suspect, in doing so, you'll realise what the problem is.

Oh woops, text was set to string and i returned a string and tried to convert that to a string... stupid mistake :P

Question 2: If you are getting individual characters, why is your Stack a Stack<String>?

I'm not entirely sure. I'm following an exercise which has provided the methods and can't be changed.

PS: I just noticed that you talked about ADT's, and Android's Stack class is NOT the same as Java's. However, that doesn't invalidate anything I said.

errrrrrrrrrrrrrr I think the adt is java's. Please move me to the beginners forum, I will post back into this thread when I run into further problems
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Don't use java.util.Stack. Use this instead.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kan Za wrote:errrrrrrrrrrrrrr I think the adt is java's. Please move me to the beginners forum, I will post back into this thread when I run into further problems

OK, done.

BTW, In proper Java, they're called APIs (Application Programming Interface), not ADTs.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought ADT meant Abstract Data Type.

You cannot put a . after an identifier if that identifier represents a primitive type.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I thought ADT meant Abstract Data Type.

Not according to this, which was the first thing I got when I Googled ADT (yours was the next ); but I was totally wrong about Android's Stack class. For some reason, I thought push() returned void, which was why I thought it was different. Seems odd that it would return the argument you gave it.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!