0

For a practice problem for my programming class we have:

"Define a method that returns the first row of a two-dimensional array of strings that has a string with the name “John”."

public class TwoDimensionalStringArrayI {

public static void main(String[] args) {
    String[][] b = {{"John", "Abby"},
                    {"Sally", "Tom"}};

    System.out.println(firstRow(b)); // line 8
}

public static String[] firstRow(String[][] a) {
    String[] name = new String[a[0].length];
    int counter = 0;

    for (int row = 0; row < a.length; row++) {
        for (int col = 0; col < a[row].length; col++) {
            name[counter++] = a[row][col]; // line 17
        }
    }
    return name;
  }
 }

After going through the debug process on Eclipse, my String array name is set to {"John", "Abby"}, however I am getting an ArrayIndexOutOfBoundsException error at lines 8 and 17 when attempting to run the program.

Confused on what to do to get this program to output the names "John" and "Abby."

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
derpt34
  • 39
  • 5

2 Answers2

0

I think you should switch the variables row and col in line 17.

  • I tried that , but it just changes the name array to "John" and "Sally." Also I still get the errors. – derpt34 Nov 21 '15 at 22:33
0

Because of this line;

for (int row = 0; row < a.length; row++) {

The goal of firstRow(String[][] a) method is returning the first row of the array, thus, the line above should be as below;

for (int row = 0; row < 1; row++) {

Because that it traverse all of the elements of the array, it exceeds the size of the name array which has only a[0].length rooms, numerically, 2. ( String[] name = new String[a[0].length]; )

In order to make your code functional, there are two ways;

First Solution

Update the for loop conditional as written above, the test code is;

public class TwoDimensionalStringArrayI {
    public static void main(String[] args) {
        String[][] b = {{"John", "Abby"},
                        {"Sally", "Tom"}};

//      System.out.println(firstRow(b)); // line 8
        String[] result = firstRow(b);
        for(int i = 0; i < result.length; i++)
            System.out.print(firstRow(b)[i] + " ");
    }

    public static String[] firstRow(String[][] a) {
        String[] name = new String[a[0].length];
        int counter = 0;

//      for (int row = 0; row < a.length; row++) {
        for (int row = 0; row < 1; row++) {
            for (int col = 0; col < a[row].length; col++) {
                name[counter++] = a[row][col]; // line 17
            }
        }
        return name;
    }
}

The output is as below;

John Abby 

You've noticed (you should), I've also updated the print line.

Second Solution

The second way to make your code functional is, returning only first row for a[][], which is actually is as easy as returning a[1]. The test code is;

    public static void main(String[] args) {
        String[][] b = {{"John", "Abby"},
                        {"Sally", "Tom"}};

//      System.out.println(firstRow(b)); // line 8
        String[] result = firstRow(b);
        for(int i = 0; i < result.length; i++)
            System.out.print(firstRow(b)[i] + " ");
    }

    public static String[] firstRow(String[][] a) {    
        return a[0];
    }

And the output is;

John Abby 

Hope that it helps.

Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106