Arraylist toString
I have created a sort of members data base where by the members add themselves along with some details. The data is saved in an arraylist and the details saved correspond to a details class. To show all the details for the members a toString is called. My problem is i cant get the program to display the details for one member. I can get it to either display the toString which is all the details or display the number of that member in the arraylist.
Does anyone know how i can go about displaying a single members details.
Any help is much appreciated
Thanks
> ...
> Does anyone know how i can go about displaying a
> single members details.
> ...
Put a method like this in your MemberDatabase class:
public String getMemberDetailsWithId(String id) {
for(Member temp : members) { // 'members' is the ArrayList holding all of your Member instances
if(temp.id.equals(id)) {
return temp.toString();
}
}
return null; // nothing found, return null
}
And 'id' is a unique attribute of your Member class.
The program is creating a GUI and the array list is accessed when a member is searched for using a JComboBox and a search button.
At the moment I have:
for(details de: detail) {
if( de.getName().equals(memBox.getSelectedItem())){
displayPane.setText(de.toString());
}
else
displayPane.setText("fail");
This will only display the first item in the array then fail for the others. Do you have any idea how I display the other items when the member is selected in the combobox.
Thanks
> ...
> is will only display the first item in the array then
> fail for the others. Do you have any idea how I
> display the other items when the member is selected
> in the combobox.
> ...
Override the toString() method in your Member class. In the toString() method you return a String of all the attributes inside you Member class. Let that String be the parameter in displayPane.setText(...).