开发者

Help with toString() - adding to collections

开发者 https://www.devze.com 2022-12-20 10:14 出处:网络
I am adding information from main() I am adding different information for CD, DVD, book.. I have 3 separate classes - item has 3 classes in it...

I am adding information from main() I am adding different information for CD, DVD, book..

I have 3 separate classes - item has 3 classes in it...

  1. project - main()

  2. Library - this function does all the adding

  3. Item(cd,dvd,book) inheritance

For Music i am adding band info, title info, keywords, and members.. I am adding members separately than of the other info..

As you can see the members is not outputing correctly as the others..

>>> music CDs:

-Music-
band: Jerry Garcia Band
# songs: 15
members: [Ljava.lang.String;@61de33
title: Don't Let Go

C:\Java\a03>

I am using the same toString() function for members as i am the rest, so i am not sure why it would do this..

I will give you as much info as i think you need to see..

Main() - as you can see it calls 2 different functions.

the addbandmembers is where i am having problems...

out.println(">>> adding items to library:\n");
item = library.addMusicCD("Europe In '72", "Grateful Dead", 12, "acid rock", "sixties", "jam bands");
    if (item != null) {
        library.addBandMembers(item, "Jerry Garcia", "Bill Kreutzman", "Keith Godcheaux");
    开发者_StackOverflow社区    library.printItem(out, item);
        }

in Library class - here is the addbandmember function ..

Could this be the cause??

public void addBandMembers(Item musicCD, String... members)
{

    ((CD)musicCD).addband(members);

}

In the Items class here is the function addband - tostring()

here is the CD class which extends the items class..

class Item
{
    private String title;

    public String toString()
    {
        String line1 = "title: " + title + "\n";
        return line1;
    }

    public void print()
    {
        System.out.println(toString());
    }

    public Item()
    {}

    public Item(String theTitle)
    {
        title = theTitle;
    }

    public String getTitle()
    {
        return title;
    }
}

class CD extends Item
{
    private String artist;
    private String [] members;
    private int number;

    public CD(String theTitle, String theBand, int Snumber, String... keywords)
    {
        super(theTitle);
        this.artist = theBand;
        this.number = Snumber;
    }

    public void addband(String... member)
    {
        this.members = member;
    }

    public String getArtist()
    {
        return artist;
    }

    public String [] getMembers()
    {
        return members;   
    }

    public String toString()
    {
        return  "-Music-" + "\n" + "band: " + artist + "\n" + "# songs: " + number + "\n" + "members: " + members + "\n"  + "\n" + super.toString() + "\n";
    }

    public void print()
    {
        System.out.println(toString());
    }
}

I do have other information in the items class like a nook class, movie class that i didnt show. I would like to keep everything the way i have it set up..

So, if the other items are printing fine than maybe its the cast in the addbandmember function thats giving me problems?


members is printing the way it is since it's an array (you can tell this by the fact its output as members: [Ljava.lang.String;@61de33 ).

Instead you need to iterate through it and print each element.

e.g.

for (String member : members) {
   ...
}     

The simplest way is to use Arrays.toString(). Alternatively append to a StringBuilder and then print to this. You can be cleverer, and use StringUtils.join() from Apache Commons Lang, which will give you more control.


Arrays don't have a useful toString() implementation. You can print out the members in a loop or use the Arrays.toString() method to do this for you:

return "-Music-" + "\n"
    + "band: " + artist + "\n"
    + "# songs: " + number + "\n"
    + "members: " + Arrays.toString(members) + "\n"
    + "\n"
    + super.toString() + "\n";
0

精彩评论

暂无评论...
验证码 换一张
取 消