I am trying to create a method which returns a single String (not String array) containing information for all objects in my array list. I know that strings are immutable so I am really struggling with how to add information from each object.
This is my method where I am trying to return a single string:
public String infoForEachItem ()
{
String info;
for (int y =0; y < items.size(); y++)
{
info = "ID: " + items.get(y).getId() + "\n" +
"Name : " + items.get(y).getName() + "\n" +
"Cost: " + items.get(y).getCost() + "\n";
return info;
}
}
As you can see, I want to create a string containing the Id, name, and cost of EACH item as a single string. It won't let me return within a for loop. Is there anyway to append something to the end of String?