Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help with the toString method  RSS feed

 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the question is different i know my issue with with the toString method so i completely erased what i had there but not sure where to go with it
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonothon Turner wrote:here i have posted question on other sites


Please provide links to the questions on these other sites.

Henry
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.java-forums.org/new-java/96121-linked-stack-when-print-does-read-desired-output.html
i have been working on this for two days now
i have done array stacks and drop stack with no issues with the toString
but this i cant seam to do it
 
salvin francis
Bartender
Posts: 1588
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is StackADT ?

Assuming that your actual requirement is :
Returns a string representation of this stack. The string has the form of each element printed on its own line, with the top most
element displayed first, and the bottom most element displayed last. If the list is empty, returns the word "empty".


You have a count member, it will tell you if the stack is empty, I suggest that you change your implementation from


to

to avoid a null check in favor of a simple value check.

As far as your toString is concerned,
You can still check isEmpty() and return "empty" right ?

I don't know what is LinearNode, so I wont be able to help you on printing it line by line.

 
salvin francis
Bartender
Posts: 1588
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the LinearNode the same class posted at :
http://stackoverflow.com/questions/31778395/how-to-add-an-element-to-the-end-of-a-linked-list ?
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it's the same linear node and stack adt and thank you for your suggestion I understand the point of that change
I am still learning a lot here I have posted many questions recently as I have recently got into a have course after 7 years of not doing any I am very rusty
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any help on this would be appreciated
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry the post on http://www.java-forums.org/new-java/96121-linked-stack-when-print-does-read-desired-output.html is my other one has the stack adt and linear node
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you still looking for suggestions for your toString() method?  I'm not sure in what situations you'd want to use toString() on a linked list, but maybe something like printing the top value and the size?

Tell us where and how you want to use the toStirng() method.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought that printing the contents of a List is a common enough activity. You might consider curtailing the printout for > 10⁹  elements, however.
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what I need it to print out
STACK TESTING

The stack contains:

empty

4

8

8

9

The size of the stack is: 3

The stack contains:

9

7

3
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the bottom part shouldn't be hard to implement.  We want toString() to return a string with several line separators in it.  We'll use "\n" for now, although there's a better way to do it.  So to start with, you want to concatenate several strings.  We'll start with a variable called result set to "" and use "+" to concatenate.  (There's also a better way to do that, but more later.)

There are several improvements we can make to this code, but you will need to fill in the commented parts first.
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried this as my toString


but my issue now is that when i run the program i get a memory error please help
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't line 13 be
next = next.getNext();
?
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get a memory error 

That could be caused by the method calling itself.  What class is top?  Is the toString method you are writing in that class?
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Wouldn't line 13 be
next = next.getNext();
?

The way you have it now, by starting off calling top.getNext() and also calling this inside the loop, you end up with an infinite loop which runs until your StringBuilder runs out of memory.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!