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."