The default toString method doesn't know how you are expecting the output. Hence the default behaviour is that it prints the type of the class and most possibly the address. If you are planning on printing the list you will have to write loop
This is the code you have written to override toString. You have also used incorrect valiable name for retuurn
in loop retur
public String toString(){
String retuurn = "";
for (GroceryItem items : item) {
if(items != null){
retuurn += item.toString() + "\n";
// Your variable name in your original question is retur, I'm assuming that as typo
}
}
return retuurn;
}
Issue with this code is it uses item.toString() which you have not created so it uses default toString method, which doesn't know which data to return. use retuurn += item.getName()+"\n";
Alternatively you can create a toString in your GroceryItem.java that returns what ever data you want returned. Sample code is as below.
public String toString(){ // inside GroceryItem.java
return this.name; // or return this.name & ";" & this.quantity; or what ever that should be returned.
}