Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Loops and Final Touches For Loops

(loop)The preview says that there is an unexpected "}" at line 9 but there isnt a one anywhere near

I have tried everything now, plz help

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            double total = 0;
            int i = 0;
            for(i <= frogs.Length; i++)
            {
                total += frogs[i];
            }
            return total/i;
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }
    }
}

now i see it cropped the line numbers... The line 9 is this --> for(i <= frogs.Length; i++) on the first picture

1 Answer

Steven Parker
Steven Parker
231,198 Points

The message is a bit misleading, but it looks like there are 3 issues:

  • the "for" loop needs an initialization clause, even if it is empty
  • the loop should stop before reaching the array "Length"
  • you can't add "frogs", add their tongue lengths instead:
            for ( ; i < frogs.Length; i++)       // empty init clause, and stop BEFORE Length
            {
                total += frogs[i].TongueLength;  // add lengths not frogs