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 Review Looping over 2d arrays

Karel Schwab
Karel Schwab
12,955 Points

Question about looping over 2D arrays

The question requires me to fill in the blanks. This is how I have it... What am I doing wrong?

char[][] boggle = { {'C', 'A', 'T'}, {'D', 'R', 'I'}, {'L', 'O', 'G'}};

System.out.printf("-------------%n"); for (int i = 0; i < boggle.length; i++) { for (int j = 0; j < boggle[0].length; j++) { System.out.printf("| %s ", boggle[i][j]); } System.out.printf("|%n-------------%n"); }

1 Answer

Yanuar Prakoso
Yanuar Prakoso
15,195 Points

Hi Karel

If you look your code you made mistake on the second for loops:

char[][] boggle = { {'C', 'A', 'T'}, {'D', 'R', 'I'}, {'L', 'O', 'G'}};

System.out.printf("-------------%n"); for (int i = 0; i < boggle.length; i++) 
{ for (int j = 0; j < boggle[0].length/*<--here is the problem*/  ; j++) 
{ System.out.printf("| %s ", boggle[i][j]); } System.out.printf("|%n-------------%n"); }

The j upper limit step is not j < boggle[0].length but it should be j < boggie[i].length like this:

char[][] boggle = { {'C', 'A', 'T'}, {'D', 'R', 'I'}, {'L', 'O', 'G'}};

System.out.printf("-------------%n"); for (int i = 0; i < boggle.length; i++) 
{ for (int j = 0; j < boggle[i].length; j++) 
{ System.out.printf("| %s ", boggle[i][j]); } System.out.printf("|%n-------------%n"); }

I hope this can help.

Karel Schwab
Karel Schwab
12,955 Points

Thank you very much.