wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Help with An Array Of Objects 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

Help with An Array Of Objects

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
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

Joined: Feb 22, 2009
Posts: 192
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
What do you mean by toString()?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39062
    
  23
David Barry wrote:What do you mean by toString()?
Look at this, and this.
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
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

Joined: Oct 13, 2005
Posts: 39062
    
  23
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

    Joined: Mar 13, 2009
    Posts: 492

    Well actually that would print out the same thing.

    Example:


    output:
    Name: Sydney Gender: F
    Name: Sydney Gender: F

    "If the facts don't fit the theory, get new facts" --Albert Einstein
    David Barry
    Ranch Hand

    Joined: Jan 13, 2009
    Posts: 83
    Thanks for the advice/help. I figured I would print it like this:

     
    GeeCON Prague 2014
     
    subject: Help with An Array Of Objects