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

Joshua Thao
Joshua Thao
6,439 Points

How would we have printed this out if the names were not in order of the scores?

If I wanted to print alena score for Ben what do I need to do in my code?

String[] friends = {"Ben", "Alena", "Pasan"};

    int[][] scoreCards = { // to get score of ben - scoreCards[0], if index of 1 - scoreCards[0][1] etc.
            {1, 2, 4, 2, 6, 5, 4, 3, 3, 2, 5, 7, 2, 7, 8, 4, 3, 2},// ben
            {2, 3, 5, 1, 1, 2, 3, 1, 1, 2, 4, 1, 3, 3, 2, 6, 3, 2},// alena
            {4, 4, 2, 1, 2, 2, 1, 4, 2, 2, 2, 3, 2, 5, 8, 1, 2, 2} // pasan score at 15th hole would be [2][14]
    };

    // for each friend print name
    for (int index = 0; index < friends.length; index++) {
        System.out.printf("%s %n-------------------------%n",
                friends[index]);
        // for each hole print score
        for (int j = 0; j < scoreCards[index].length; j++){
            System.out.printf("Hole #%d: %d %n",
                    j + 1,
                    scoreCards[index][j]);
        }
    }

1 Answer

Zhaopeng Wang
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Zhaopeng Wang
Full Stack JavaScript Techdegree Graduate 32,210 Points
  • if you want to print Alena's score before Ben, you can do a swap in scorecards
  • Also, you need to modify the String[] friends and let Alena be the first
String[] friends = {"Alena", "Ben", "Pasan"};
        int[][] scoreCards = { // to get score of ben - scoreCards[0], if index of 1 - scoreCards[0][1] etc.
                {1, 2, 4, 2, 6, 5, 4, 3, 3, 2, 5, 7, 2, 7, 8, 4, 3, 2},// ben
                {2, 3, 5, 1, 1, 2, 3, 1, 1, 2, 4, 1, 3, 3, 2, 6, 3, 2},// alena
                {4, 4, 2, 1, 2, 2, 1, 4, 2, 2, 2, 3, 2, 5, 8, 1, 2, 2} // pasan score at 15th hole would be [2][14]
        };

        int[] temp = new int[18];
        temp = scoreCards[0];
        scoreCards[0] = scoreCards[1];
        scoreCards[1] = temp;

        // for each friend print name
        for (int index = 0; index < friends.length; index++) {
            System.out.printf("%s %n-------------------------%n",
                    friends[index]);
            // for each hole print score
            for (int j = 0; j < scoreCards[index].length; j++) {
                System.out.printf("Hole #%d: %d %n",
                        j + 1,
                        scoreCards[index][j]);
            }
        }