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 trialKarel Schwab
12,955 PointsQuestion 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
15,196 PointsHi 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
12,955 PointsKarel Schwab
12,955 PointsThank you very much.