0

I have this 2d array of the first row being first names and the second being last names.

String[][] ladrones2 = {
        {"Phil", "Kim", "Phil", "Perry"},
        {"Garcia", "Gimena", "Basinger", "Ornitorrinco"}};

the prefered out come of this would be to have them printend out like e.g

Phill Garcia
Kim Gimena
etc...

Now I was able to do this by catching the ArrayIndexOutOfBoundsException exception and it works and the output is:

name Phil Garcia
name Kim Gimena
name Phil Basinger
name Perry Ornitorrinco

Which is great but I wondered if there was a way to do this without having to catch the exception? I've been searching to make this cleaner but wasn't able to do so.

Code at the moment:

public static void robo2() {
    String[][] ladrones2 = {
            {"Phil", "Kim", "Phil", "Perry"},
            {"Garcia", "Gimena", "Basinger", "Ornitorrinco"}};

    try {
        for (int i = 0; i < ladrones2.length; i++) {
            for (int j = 0; j < ladrones2[i].length; j++) {
                System.out.println("name: " + ladrones2[i][j] + " " + ladrones2[i + 1][j]);
            }
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("");
    }
}

as you can see I used in the inner for loop a +1 on the i variable which in turn throws the exception.

Any ideas? I would like just to keep this way of working at the moment with 2 forloops due to being new to Java.

2 Answers 2

1

If you are sure that ladrones2 array always has 2 rows, then below code would be simpler:

public static void main(String[] args) {
    String[][] ladrones2 = {
            {"Phil", "Kim", "Phil", "Perry"},
            {"Garcia", "Gimena", "Basinger", "Ornitorrinco"}};

    for (int i = 0; i < ladrones2[0].length; i++) {
        System.out.println("name: " + ladrones2[0][i] + " " + ladrones2[1][i]);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Sundhir , nice I got the same output thanks for the advice. I just have one more question I don't seem to understand here. Why are we still putting [0] after ladrones2 in the for loop? Wouldn't that not be used only on the inner for loop if you would use one ? Thanks for the help already.
@SamuelVanBladel [0] gives name count in each row. This is required to stop the loop once all names are printed. Hope this helps.
Hi Sudhir, thanks for the quick reply but what do you mean with "name count" ?
@SamuelVanBladel "name count" - number of elements (names) in each row of the 2d array
1

On this line :

System.out.println("name: " + ladrones2[i][j] + " " + ladrones2[i + 1][j]);

You are taking an element from the first and second array at the same time but you are looping over all the elements in the sequence on this line :

for (int i = 0; i < ladrones2.length; i++) 

To correct this problem, you simply have to reduce the range of i to ladrones.length-1, otherwise you will reach the end of the array and try to access the next element which doesn't exist here.

You'll end up with this line instead :

for (int i = 0; i < ladrones2.length-1; i++) {

2 Comments

Hi Simon, I've added the -1 to my outer for loop but now I get the following output. name: Phil Garcia name: Kim Garcia name: Phil Garcia name: Perry Garcia As yiu can see the second row gets stuck on 1 - 0
You probably did something wrong here. If you followed my advices, you should have written the following code : Java for (int i = 0; i < ladrones2.length-1; i++) { for (int j = 0; j < ladrones2[i].length; j++) {System.out.println("name: " + ladrones2[i][j] + " " + ladrones2[i + 1][j]); } }

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.