0
public String foodstats (){
    String foodstats = "";
    for ( int i = 0; i < consumables.size(); i++){
         foodstats =  "Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n" ;
   }
    return foodstats;
}

So this returns: Name: Water Nutrition: 30 Stamina : 15

I realize why its doing this, the second time the for loop runs through it replaces the first item's stats and returns only the replaced stats.

Is there a way around this? I need to return all the item's stats based on the array list size.

4
  • What is the desired output Commented Jan 8, 2015 at 2:31
  • 3
    It's a little hard to understand exactly what you're aiming for here, but it seems like you probably want to use += instead of =. (But my mind-reading skills are a little off today.) Commented Jan 8, 2015 at 2:31
  • 2
    Or you want to return a List<String> instead of a String. Commented Jan 8, 2015 at 2:32
  • Basically its replacing the first item stats with the second, the desired output would be: Stats of Soup item, then stats of water as above Commented Jan 8, 2015 at 3:02

3 Answers 3

1

I think what you are looking for is a StringBuilder, more efficient in that case than += concatenation :

public String foodstats (){
    StringBuilder foodstats = new StringBuilder();
    for ( int i = 0; i < consumables.size(); i++){
         foodstats.append("Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n");
   }
    return foodstats.toString();
}
Sign up to request clarification or add additional context in comments.

2 Comments

This works perfectly. Although I'm curious what does .append do?
A StringBuffer holds an array of chars and expand its size when necessary. Basically, you can consider it as an arraylist of chars. Thus, append just tries to write the characters of the input string in the underlying array. If there is not enough space, a bigger array is allocated, the previous array is copied in it and finally the chars of the input string are written at the end
1

Make a StringBuilder and use append(...) in the loop. Once you are done, call toString() on the result, like this:

StringBuilder res = new StringBuilder();
for ( int i = 0; i < consumables.size(); i++){
     res.append("Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n");
}
return res.toString();

Note: you could use foodstats += ... syntax, too, but that would be inferior, because each iteration of the loop would create a throw-away temporary object.

Comments

1
public String foodstats()
    {
        StringBuilder foodstats = new StringBuilder();
        for (int i = 0; i < consumables.size(); i++)
        {
            foodstats.append("Name: "); 
            foodstats.append(consumables.get(i).getName());
            foodstats.append("\tNutrition: ");
            foodstats.append(consumables.get(i).getNValue());
            foodstats.append("\tStamina: ");
            foodstats.append(consumables.get(i).getStamina());
            foodstats.append("\n");
        }
        return foodstats.toString();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.