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

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]); 
    }


  }

2 Answers

Aslak Wold
Aslak Wold
1,604 Points

If you didn't get it to work (I expect you did from you comment above, but) the reason is probably because of the curly-bracket you have after having made the friends array. So you've effectively put you code outside of the main method. Other than that everything looks good i believe.

Haha, thanks for the help. I noticed it after a while but this can be good for future people to see.

alastair cooper
alastair cooper
30,617 Points

Have a look at you first declaration of int[][] scorecards. It is cut off with a class declaration half way through it.

In the loop below you have a semicolon after the final bracket!

Hehe, just noticed I did a very bad job of copying the code but it should be right now. And as for the semicolon after the final bracket, Craig used it for his video also and when I tried without the final bracket I got an error about it expecting a semicolon.