Reputation: 45
I need to print the name of the shop (this method is in the Shop class) as well as the product name (which is an arraylist) and the price of the product ( which is done using a getter from the class Product). I want to loop through all the products in the arraylist, how can I do that ?
Please if something is not clear comment it and I will edit the question to make it clearer.
public String toString()
{
for(int i=0; i<products.size();i++)
{
return "Shop"+"["+"name"+" = "+name+","+"Product name"+" = "+products.get(i).getName()+", "+"Price"+" = "+products.get(i).getPrice()+"]";
}
return null;
}
This is wrong as the i++ is a dead code which means that the loop will execute once. Any help please ? Thanks for your time
Upvotes: 0
Views: 11630
Reputation: 11
If you have a small collection, you can always just concatenate strings with +.
Creating a StringBuilder provides unnecessary overhead for small examples.
Other than that, compiler will optimise that part of code If it provides improvement to the performance.
public String toString() {
String str = "Shop [name = " + name + " ";
for (Product product : products) {
str += ", Product name = " + product.getName();
str += ", Price = " + product.getPrice() + "]";
}
return str;
}
Upvotes: 1
Reputation: 39477
Use StringBuilder
:
public String toString() {
StringBuilder sb = new StringBuilder("Shop").append("[name = ").append(name)
for (Product product : products) {
sb.append(",")
.append("Product name = ").append(product.getName())
.append(", Price = ").append(product.getPrice())
.append("]");
}
return sb.toString();
}
Do not use +
inside the loop to join strings (as accepted answer did) as it will create a stringbuilder in each iteration to join the strings, and then append it to the outer stringbuilder.
Upvotes: 5
Reputation: 4801
You can use Guava Objects Class it facilitates to build the object toString().They changed it recently to MoreObjects.ToStringHelper
public String toString(){
ToStringHelper toStringHelper = MoreObjects.toStringHelper(this);
for (Product p : products)
{
toStringHelper.add("name", p.name);
toStringHelper.add("Product name", p.getName();
}
return toStringHelper.toString();
}
Upvotes: 1
Reputation: 127
You could use StringJoiner ( https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html )
public String toString(){
StringJoiner string = new StringJoiner(", ", name + "[", "]");
for (Product p : list) {
string.add("Product name:" + p.getName()).add("Product value:"+p.getValue());
}
return string.toString();
}
Output:
Shop[Product name:Teste, Product value:1.0, Product name:teste2, Product value:2.0]
Upvotes: 1
Reputation: 2949
Using a return
statement will end the function's execution. Here's what you want to do instead.
public String toString()
{
StringBuilder result = new StringBuilder();
result.append("Shop [ name = "+name+" ");
for(Product p : products)
{
result.append("Product name"+" = "+p.getName()+", "+"Price"+" = "+p.getPrice()+" ");
}
result.append("]");
return result.toString();
}
Upvotes: 1