7

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.

4
  • The inner loop will allow j to be 2 which is out of the bounds of the inner array. In other words Preferences[0][2] is out of the bounds of the array because the length is 2. Inner loop should be j < 2, not 3. Or more reliably Preference[i].length. Commented Aug 1, 2013 at 14:52
  • and the answers begins...... Commented Aug 1, 2013 at 14:52
  • Why is i < 2 and j < 3 ? Commented Aug 1, 2013 at 14:52
  • Than you guys very much, I had changed that earlier from >2 to >3 to attempt to rid myself of 'nullnull' being printed. Cheers! :) Commented Aug 1, 2013 at 14:57

11 Answers 11

11

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]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2
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]);}
    }
     }  

Comments

1

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

Comments

1
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

Comments

1

Simple

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

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

1 Comment

Thank you very much, that has ridded me of the error but the return is now Tom,Cokenullnull,John Pepsi,
1

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.

Comments

1

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]);
        }
    }

Comments

0

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

Comments

0

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++)

Comments

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

Output:

[Tom, Coke, null]

[null, John, Pepsi]

1 Comment

Welcome to SO! It is always a good idea to provide some explanation along with your answer code. People looking for a solution will more likely understand your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.