fritz-playmaker
fritz-playmaker

Reputation: 751

Outputting values in 2d array not working

I've tried several times but to no avail it's only outputting the first row. It's a 2D array, and it's supposed to output 4 rows. It's not showing any errors. I think the logical error may be from the for loops which contain the output statements.

package aitiDay5;

public class WorldCup {

    public static void main(String[] args){
        int i=0,j=0;

        String [][] countries = new String [6][];
        countries[0]=new String[4];
        countries[1]=new String[4];
        countries[2]=new String[4];
        countries[3]=new String[4];
        countries[4]=new String[4];
        countries[5]=new String[4];
        countries[6]=new String[4];

        countries[0][0]="Countries ";
        countries[0][1]="Rival ";
        countries[0][2]="Grade ";
        countries[0][3]="Points ";

        countries[1][0]="Ghana";
        countries[1][1]="Germany";
        countries[1][2]="C";
        countries[1][3]="3";

        countries[2][0]="Croatia";
        countries[2][1]="Greece";
        countries[2][2]="B+";
        countries[2][3]="A-";

        countries[3][0]="USA";
        countries[3][1]="Portugal";
        countries[3][2]="B+";
        countries[3][3]="4";

        countries[4][0]="Spain";
        countries[4][1]="Costa Rica";
        countries[4][2]="F+";
        countries[4][3]="-2";

        countries[5][0]="Brazil";
        countries[5][1]="Germany";
        countries[5][2]="Z";
        countries[5][3]="-32";

        countries[6][0]="Argentina";
        countries[6][1]="Holland";
        countries[6][2]="A";
        countries[6][3]="10";

        for ( i=0;i<=countries.length;i++){
            System.out.println("\n");

            for(j=0;j<countries.length;j++){
                System.out.println(countries[i][j]+ " ");
            }
        }
    }
}

Upvotes: 2

Views: 215

Answers (4)

Jason Baker
Jason Baker

Reputation: 2483

Your inner for loop statement should be for(j = 0; j < countries[i].length; j++), and your outer for loop should be for(i = 0; i < countries.length; i++).

As it stands you're not fully looping through the second dimension. I'm actually very surprised that you're not seeing an error, because based on your arrays you should have a ton of illegal access exceptions. Java gets really upset when you try to access past the end of an array (example: your inner arrays only contain 4 elements, but your inner for loop iterates until j=5, meaning you're accessing the 5th element in a 4-element array)

Upvotes: 0

Andrew_CS
Andrew_CS

Reputation: 2562

Your first for loop statement should just use the < operator, not the <= operator.

Your second loop should index into the row to check the length.

for (i = 0; i < countries.length; i++){
    System.out.println("\n");
    for(j = 0; j < countries[i].length; j++){
        System.out.println(countries[i][j] + " ");
    }
}

Also, since it appears you want 7 rows, you need to change

String [][] countries = new String [6][];

to

String [][] countries = new String [7][];

Side Note: you don't have to have i and j outside of the for loops. You can do this:

for(int i = 0; ......){ ... }

Upvotes: 2

Kevin Zaki
Kevin Zaki

Reputation: 146

The problem is simple and you are correct, there is a logical error in your nested for loop.

You are running it against the length of the entire array it should be changed to:

for(j=0;j<countries[i].length;j++){
        System.out.println(countries[i][j]+ " ");
}

Upvotes: 0

jcjunction
jcjunction

Reputation: 325

YOu need to change this line

 for(j=0;j<countries.length;j++){
        System.out.println(countries[i][j]+ " ");

To this:

 for(j=0;j<countries[i].length;j++){
        System.out.println(countries[i][j]+ " ");

Otherwise it will just loop on the single array twice

Upvotes: 0

Related Questions