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

bad operand type

public class Explore {

public static void main(String[] args) { // Create a new friends array using the array literal shortcut. // Include Ben, Alena, Pasan String[] friends = {"Ben", "Alena", "Pasan"}; // Enhanced for loop 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.printf("%s___________%n",friends[i]); for (int j=0;j < scoreCards[i].length;j++){ System.out.printf("hole #%d : %d %n",j+1,scoreCards[i][j]); } }

}

}

it is saying that in the 2nd for loop i am using a bad operand type

1 Answer

Steven Parker
Steven Parker
217,574 Points

Without formatting, most of the program appears as a single-line comment. But after reconstructing, it complied and ran without errors. Perhaps the original has some issue with the way it's laid out?

Here's the reconstruction shown with Markdown formatting:

public class Explore {
    public static void main(String[] args) {
        // Create a new friends array using the array literal shortcut.
        // Include Ben, Alena, Pasan
        String[] friends = {"Ben", "Alena", "Pasan"};

        // Enhanced for loop
        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.printf("%s___________%n", friends[i]);
            for (int j=0; j < scoreCards[i].length; j++) {
                System.out.printf("hole #%d : %d %n", j+1, scoreCards[i][j]);
            }
        }
    }
}