Need to read a set of text string files into a 2D array. The text string format looks like this, each line ends with "\n" with various length
"dog", "runs", "fast"
"birds", "flies", "high"
"baby", "cries", "often", "in the evening"
"He", "works"
....
Would like to get the 2D array output below:
{ {"dog", "runs", "fast"}, {"birds", "flies", "high"},
{"baby", "cries", "often", "in the evening"}, {"He", "works"},
...
}
Thinking to use StringBuilder to read each line from a file and append it to a 2D Object [][] array (but used String [][] instead). The following codes are my initial attemps - not pretty, but not working either.
import java.io.*;
import java.util.*;
public class My2DArrayTest
{
public static void main(String args[])
{
String[][] myString = new String[4][3];
try
{
FileReader file = new FileReader("MyTestFile.txt");
BufferedReader reader = new BufferedReader (file);
String strLine;
String EXAMPLE_TEST;
for (int row = 0; row < 4; row++) {
for (int column = 0; column < 3; column++) {
while ((strLine = reader.readLine()) != null{
if (strLine.length() > 0) {
EXAMPLE_TEST = strLine;
System.out.println ("This is EXAMPLE_TEST: " +
EXAMPLE_TEST);
myString[row][column]=EXAMPLE_TEST;
System.out.println("Current row: " + row);
System.out.println("Current column: " + column);
System.out.println("This is myString Array:" +
myString[row][column] + " ");
}
}
}
}
file.close();
} catch( IOException ioException ) {}
}
}


toArray()on the ArrayList at the end to get the full array."a", "b", "c"? Or isa b cora,b,corabcetc?