I am trying to create a method that with open a file then read the file line by line no matter the length of file. Each line is split into words and punctuation (excluding the apostrophe) and stored in an ArrayList of Strings. These ArrayLists representing the line are stored in an ArrayList of ArrayLists of Strings.
For example if the file contains.
Hello World!
Hi, hello.
Then the first array list would contain the strings: "Hello", "World", "!" The second array list would contain the strings: "Hi", ",", "hello", "."
So far I have the following and am not sure if I am going in the right direction.
public static void readInputFile(String fileName, ArrayList<ArrayList<String>> fileByLine)
throws IOException {
File userFile = new File(fileName);
try {
if (userFile.exists()) {
Scanner fileScanner = new Scanner(userFile);
do {
do {
fileByLine.add(fileScanner.next());
} while (fileScanner.next() != null);
} while (fileScanner.hasNext());
}
} catch (Exception e) {
System.out.println("Exception: File '" + fileName + "' not found.");
}
}