Reputation:
I am trying to add information from main() to a items class where i am storing the information in a hashset
i have 3 classes
project - main()
libaray - addBandMembers function
Item - addband(String... member)
i am adding CD information. first, i add band, # of songs, title - which works good
then in another function i am trying to add the band members. I want to keep these separate. Where i am having problems is assign the band members..
I understand that how Varargs works just not sure how to assign that to a string..
I will only show bits of code to keep this post simple and short..
this is what i have:
Main()
item = library.addMusicCD("Don't Let Go", "Jerry Garcia Band", 15, "acid rock", "jam bands");
if (item != null) {
library.addBandMembers(item, "Jerry Garcia", "Keith Godcheaux");
library.printItem(out, item);
}
Then, here the first function thats called..
public void addBandMembers(Item musicCD, String... members)
{
//musicCD.addband(members); // both cant find addband function..
//Item.addband(members);
}
Then in another class i am trying to add the information..
private String members;
public void addband(String... member)
{
this.members = member; // error
}
if i make the private String members; an array then this function below errors.. incompatible type..
public String getMembers()
{
return members;
}
How can i do this?
oh ya, here is my set..
public class Library
{
private Set<CD> theCDs = new HashSet<CD>();
private Set<DVD> theDVDs = new HashSet<DVD>();
private Set<Book> theBooks = new HashSet<Book>();
if you need to see more code let me know.. Thank you so much..
Upvotes: 4
Views: 2377
Reputation: 1108692
A varargs argument resolves to an array.
So, change
private String members;
to
private String[] members;
Upvotes: 2
Reputation: 91871
String... member gives you an array, so what you are looking at is a member that is of type String[].
I think once you get that, you kind of get over the hump of the issue.
Upvotes: 1
Reputation: 12646
Change your getter to return a String array instead of a String.
Upvotes: 1