Dan Wyer
Dan Wyer

Reputation: 161

Using a for loop to Print and Two-Dimensional Array in Java

I am trying to complete an assignment (so point in the general direction would help greatly) within which I have to (in order):

  1. Declare a 2d String Array,
  2. Assign Values to the array of two people and their favourite drink
  3. Output using a for loop

public class doublearray {
    public static void main(String[] args){
        String Preferences [] [] = new String [2][2];
        Preferences [0][0]= "Tom, Coke";
        Preferences [1][1]= "John, Pepsi";

        for (int i=0; i<2; i++){
            for (int j =0; j<3; j++){
                System.out.print(Preferences[i][j]);
            }
        }
    }   
}

I receive this error message

Tom, CokenullException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at doublearray.main(doublearray.java:15)

Now, I understand that ",Tom,Coke" have been assigned only to ONE [0] which is why null appears, but I've no idea how to remedy that or make it print successfully.

Any help would be most appreciated, I've been stuck on this for about an hour. Thank ya'll.

Upvotes: 7

Views: 46230

Answers (11)

Emmanuel Chukwu
Emmanuel Chukwu

Reputation: 11

Try this.

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

Upvotes: 1

VeryMery
VeryMery

Reputation: 1

    for (String[] row : Preferences) {
        System.out.println (Arrays.toString(row) );
    }

Output:

[Tom, Coke, null]

[null, John, Pepsi]

Upvotes: 0

Michael Laffargue
Michael Laffargue

Reputation: 10294

You may want something like that :

Preferences [0][0]="Tom";
Preferences [0][1]="Coke";
Preferences [1][0]="John";
Preferences [1][1]="Pepsi";

You'll know that Preferences[0] is about Tom
You'll know that Preferences[1] is about John

And once you have it, the columns will be [0]=>"name" [1] =>"drink"

[0][1] will give you Tom[0] s drink[1] [Coke] for example.  
[0][0] will give you Tom[0] s name[0] [Tom] for example.
[1][1] will give you John[1] s drink[1] [Pepsi] for example.  
[1][0] will give you John[1] s name[0] [John] for example.

Upvotes: 4

Narendra Pathai
Narendra Pathai

Reputation: 41945

for (int i=0; i<2; i++){
  //size for inner loop was 3 but should be 2      
  for (int j =0; j<2; j++){

    System.out.print(Preferences[i][j]);}
}
 }  

For arbitrary size

for (int i=0; i<Preferences.length; i++){
      for (int j =0; j<Preferences[i].length; j++){

        System.out.print(Preferences[i][j]);}
    }
     }  

Upvotes: 2

Ilesh Patel
Ilesh Patel

Reputation: 45

How can you take your j from 0 to 2 since your array length is 2 and you start your loop from 0.

change for (int j =0; j<3; j++) to for (int j =0; j<2; j++)

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236004

Try this, it's the correct way to traverse a two-dimensional array of arbitrary size:

for (int i = 0; i < Preferences.length; i++) {
    for (int j = 0; j < Preferences[i].length; j++) {
        System.out.print(Preferences[i][j]);
    }
}

Upvotes: 11

Juned Ahsan
Juned Ahsan

Reputation: 68715

You have defined your 2D array as:

new String [2][2];

and your loop seeems to be tring to fetch the elements such as

new String [0][3]; and so on because of your inner for loop:

    for (int j =0; j<3; j++)

Leading to array index out of bound. You may need to change the inner for loop to

    for (int j =0; j<2; j++)

and try.

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

Simple

 for (int i=0; i<2; i++){
        for (int j =0; j<2; j++){

    System.out.print(Preferences[i][j]);}
}

Upvotes: 1

Michael Yaworski
Michael Yaworski

Reputation: 13483

for (int j =0; j<3; j++){

needs to be

for (int j =0; j<2; j++){

You didn't make the array big enough for j to be == 2 so it's out of bounds

Upvotes: 1

Prabhaker A
Prabhaker A

Reputation: 8473

In second loop j should also j<2 instead of j<3

Upvotes: 1

Loki
Loki

Reputation: 4130

Look at your second for-loop. Consider you only have space for 2 Values per final dimension

Upvotes: 0

Related Questions