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 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,196 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.