• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return a string in a while loop  RSS feed

 
Garry Tunderwood
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Here, I want to avoid using the line **return"";** and instead return all of the values I output with **System.out.println();**

I have tried using a **StringBuilder** method, but this outputs nothing when I run my test program. I am guessing that StringBuilder must work differently in a while loop, but I can't find how!

Any advice or guidance would be appreciated!
http://www.coderanch.com/forums/posts/zeroReplies
Thanks a lot,
Garry

 
Carey Brown
Saloon Keeper
Posts: 3311
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are misusing the toString() method. It should not print anything, it should only return a string, and, in your case, you will probably create the string by using a StringBuilder object. If what you're wanting is to print something for the purpose of debugging I suggest creating a different method which could possibly make use of the toString() method.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have removed some of the excess blank lines from your post; they don't help legibility.
I am afraid that is not a correct overriding of the toString method. It is supposed to return a human‑legible representation of your object. The compiler won't notice that you are returning an empty String, but people who try to display your object like this:-
System.out.println(myObj);
most certainly will notice. They will notice even more if you have a loop printing things in between. You obviously need a method to return a description and you want a method to print things out. The toString method must return a String because the superclass method (in Object) returns a String. The simplest way to get an object to print itself might be like this:-But that is rather begging the question of what you put in your toString method.

Avoid \n unless somebody had told you they want an LF character, despite what you find in some books. Rather than print("\n"); use println();

Watch this space: Further post to follow in a few minutes.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your toString method looks complicated and it is a bad idea to try out something new on something complicated. Try writing a toString method for things simpler. Start with something really simpleThen work up to something more complicated.
You can use the + operator on Strings as many times as you like, in fact the more the better, but only in one statement. Once you have multiple statements with + used on Strings, you start getting slower performance. So you use StringBuilder instead. You didn't say how you used the StringBuilder but its scope should straddle the loop. Did you create new StringBuilder objects inside the loop? That won't work.
Try it on a class with more complicated data structures:-
  • 1: That is a different way to get the correct line end for your current operating system.
  • 2: When you call append(b) that calls the toString method on b behind the scenes.
  • 3: Most methods of StringBuilder return the same object so you can chain method calls like that.
  • 4: Always use the Override annotation before anything you think you are overriding; it helps find subtle errors.
  • I think once you have worked out how to get a toString method on that Foo class you will be able to do it for your current class.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:But that is rather begging the question of what you put in your toString method...

    @Garry: And one of the things to remember there is that toString() is generally used for debugging; not for display.

    A few "rules" that might help you:
    1. It should never throw an Exception.
    2. It should always print out something; even if it's only to say that the object is "empty" (or that there was an error).
    3. It should print out the entire contents of the class where possible (including member objects that may themselves have toString() methods).
    4. It should not try to be "pretty" (and I think this is your basic problem); but it should be as "readable" as possible.
    5. (As Campbell said) It should not contain newlines.
    6. (Tip) Enclose the String in brackets. "[...]" are quite commonly used. This will help to delimit objects "visually" if you ever need to print out several at once (or your class contains embedded ones written by you). You might even want to add a single space at the end of the String.

    As you can see, there's quite a lot to know; and I suspect there's no "magic bullet" for writing the perfect toString() method. But the main thing to remember is that it's not a "display" or "report" method; it's a "what was in this class" one.

    HIH

    Winston
     
    Dave Tolls
    Ranch Foreman
    Posts: 3056
    37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Campbell Ritchie wrote:But that is rather begging the question of what you put in your toString method...

    @Garry: And one of the things to remember there is that toString() is generally used for debugging; not for display.

    A few "rules" that might help you:
    1. It should never throw an Exception.
    2. It should always print out something; even if it's only to say that the object is "empty" (or that there was an error).
    3. It should print out the entire contents of the class where possible (including member objects that may themselves have toString() methods).
    4. It should not try to be "pretty" (and I think this is your basic problem); but it should be as "readable" as possible.
    5. (As Campbell said) It should not contain newlines.
    6. (Tip) Enclose the String in brackets. "[...]" are quite commonly used. This will help to delimit objects "visually" if you ever need to print out several at once (or your class contains embedded ones written by you). You might even want to add a single space at the end of the String.

    As you can see, there's quite a lot to know; and I suspect there's no "magic bullet" for writing the perfect toString() method. But the main thing to remember is that it's not a "display" or "report" method; it's a "what was in this class" one.

    HIH

    Winston


    Bit of a nitpick, but it shouldn't print out anything.
    The String that is returned should fulfill rules 2 and 3, however.
     
    Campbell Ritchie
    Marshal
    Posts: 56534
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote: . . .
    @Garry: And one of the things to remember there is that toString() is generally used for debugging; not for display.
    . . .
    Are you sure about that?
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Are you sure about that?

    Well, maybe not explicitly; but since it's what gets output by System.out.println(), I think it's a reasonable assumption that people will want to use it that way.

    What it is NOT for (IMO) is for producing "pretty" or formatted "display" versions of the class. Indeed, even documenting the format can tie you into something that you can't change later on, even if you want to.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dave Tolls wrote:Bit of a nitpick, but it shouldn't print out anything.

    Very true.

    @Garry: And just for clarification, what Dave means is that I should have said "return a String" rather than "print out".

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