I have tested and here is an example:
public static void main(String[] args) {
BufferedReader lnr = null;
String line = null;
int N=1000;
int totalRecords = 0;
String [][] array = new String[N][];
try {
lnr = new BufferedReader (new FileReader("accountList.txt"));
for (line = lnr.readLine(); line!=null; line = lnr.readLine()) {
array[totalRecords] = line.split(" ");
totalRecords++;
}
lnr.close();
} catch (Exception e) {
System.out.println("An error has occured: "+e.getMessage());
//e.printStackTrace();
}
for (int i=0;i<totalRecords;i++) {
for (int j=0;j<array[i].length;j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
2-D arrays in java don't exist. Actually what you do is create a single array and each element has another array:
String [][] array = new String[N][]; //2-D array with N rows
array[totalRecords] = line.split(" "); // the row: totalRecords will have the array returned by the split method. So the
array[totalRecords] has another array inside it and to access it you do:
array[totalRecords][0],
array[totalRecords][1],
array[totalRecords][2]
You initiall set the max row to be N=1000 because you don't know the size of the file. But you use the totalRecords variable to count the lines, because inside the loop you don't an int variable so you must do:
array[totalRecords] = ...
totalRecords++
in order to place the next line to the next row.
When you print the values you don't use the N at the first loop because not all rows of the array have value. You use the totalRecords. Remember you have used to count the lines read in the first loop.
The second inner loop you say:
array[i].length because the array[i] is also a single array and in the inner loop you print its values:
array[i][0], array[i][1] : array[i][j]