Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
    Bookmark Topic Watch Topic
  • New Topic

Help with An Array Of Objects

 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
I have an array of objects. I am wondering how to print out the values of the array. Below you will see my code. I need some help with this, please.

 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Report post to moderator
 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
What do you mean by toString()?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48932
60
  • Mark post as helpful
  • send pies
  • Report post to moderator
David Barry wrote:What do you mean by toString()?
Look at this, and this.
 
David Barry
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Report post to moderator
Ok. I think I understand what this is supposed to. I am just not getting how to correctly implement in my code. I have tried several things. Am I close with this?

 
Campbell Ritchie
Sheriff
Pie
Posts: 48932
60
  • Mark post as helpful
  • send pies
  • Report post to moderator
Yes that should work, but a few minor enhancements . . .
  • Miss out the 1. That would suggest you want your print-out to be 1 character wide.
  • If you are using printf, it is probably better to use %n which produces a system-dependent output, than \n.
  • In the code you quoted, you can leave out the toString() bit. You just write fillup[0] and it calls toString automatically.
  • You will of course have to write something in the fillup toString() method.
     
    Hunter McMillen
    Ranch Hand
    Posts: 492
    Firefox Browser Linux VI Editor
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Well actually that would print out the same thing.

    Example:


    output:
    Name: Sydney Gender: F
    Name: Sydney Gender: F
     
    David Barry
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Thanks for the advice/help. I figured I would print it like this:

     
      Bookmark Topic Watch Topic
    • New Topic