Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Arrays Iteration Looping Over 2d Arrays

Illegal start of type for loop.

So I was following along and doing everything step by step but when I finished I got an error about an illegal start of type for my for loop and it lead to me getting 46 errors!!!

I later went to the internet to search up what was wrong and a common answer I found was that a for loop must be inside of a method, a constructer or insede of a pair of curly brackets. But, in the video he didn't but the for loop inside any of them so why did his code continue to work? Here's my code

public class Explore {

  public static void main(String[] args) {

    String[] friends = { "Ben", "Alena", "Pasan"};

  }

 int [][] scoreCards= { 
{1, 2, 4, 2, 6, 5, 4, 3, 3, 2, 5, 7, 2, 7, 8, 4, 3, 2},
{2, 3, 5, 1, 1, 2, 3, 1, 1, 2, 4, 1, 3, 3, 2, 6, 3, 2},
{4, 4, 2, 1, 2, 2, 1, 4, 2, 2, 2, 3, 2, 5, 8, 1, 2, 2}
 };

  for (int i = 0; i < friends.length; i++) {
    System.out.println("%s %n----------------------%n",
                       friends[i];)
      for (int j = 0; j < scoreCards[i].length; j++) {
         System.out.println(" Hole #%d: %d %n",
                            j+1,
                            scoreCards[i][j]); 
    }


  }

1 Answer

Tabatha Trahan
Tabatha Trahan
21,410 Points

You closed out your main method before the bulk of your code. If you move the closing curly brace after the first string array you initialized, and put it after the for loop, it should work, or at least get rid of that error.

Thanks for the help, also for anyone in the future checking the answer change from println to printf instead and you will miss another error.