I'm trying to write the following method- to summarize it's supposed to take a file and parse individual words and punctuation (not apostrophe's)from the string into arraylist of arraylist. I've been struggling with this for hours and can't find anything that would help me understand where I'm going wrong - any help would be appreciated:
/**
* Opens and reads the contents of the input file specified in fileName. The input file is read
* line by line. Each line is split into words and punction (excluding the apostrophe) and
* stored in an ArrayList of Strings. These ArrayLists representing the line are stored in an
* ArrayList of ArrayLists of Strings. Specifically, they are put in the ArrayList fileByLine
* that is passed in as a parameter.
*
* For example, a file containing the following:
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Don'ec elementum tortor in mauris
* consequat vulputate.
*
* Would produce an ArrayList of ArrayLists containing 2 ArrayLists of Strings.
* The first ArrayList would contain:
* "Lorem", "ipsum", "dolor", "sit", "amet", ",", "consectetur", "adipiscing", "elit", ".",
* "Don'ec", "elementum", "tortor", "in", "mauris"
* The second Arraylist would contain:
* "consequat", "vulputate", "."
*
* Note 1: The text file is assumed to be UTF-8.
* Note 2: There are no assumption about the length of the file or the length of the lines.
* Note 3: All single quotes (') are assumed to be apostrophes.
*
* When opening the file, any FileNotFoundException is caught and the error message
* "Exception: File 'fileName' not found." followed by a new line is output,
* where fileName is the name of the file that the method attempted to open.
*
* @param fileName The name of the input text file to parse.
* @param fileByLine Reference to ArrayList to contain the contents of the file line by line,
* where each line is an ArrayList of Strings.
* @throws IOException if an I/O error occurs when closing the file. FileNotFoundException is
* caught when opening the file.
*/
public static void readInputFile(String fileName,
ArrayList < ArrayList < String > > fileByLine) throws IOException {
FileInputStream fileByteStream = null;
Scanner inFS = null;
ArrayList < String > splitByWord = new ArrayList < String > ();
String line = "";
String word = "";
//trying to open the file
try {
fileByteStream = new FileInputStream(fileName); //May throw file not found exception
inFS = new Scanner(fileByteStream);
//Loop that cycles through the file
while (inFS.hasNextLine()) {
line = inFS.nextLine();
for (String wordPunctuation: line.split("//s")) { //splits each line by whitespace
word = "";
for (int i = 0; i < wordPunctuation.length(); i++) { //splits punctuation from each word
Character letter = wordPunctuation.charAt(i);
if (Character.isLetter(letter) || letter == '\'') {
word = word + letter;
}
//else {
//word = letter.toString();
//}
}
splitByWord.add(word); //adds each word to an ArrayList
}
fileByLine.add(splitByWord); //adds the above created ArrayList to an ArrayList
}
} catch (FileNotFoundException excpt) {
System.out.print("Exception: File 'fileName' not found.\n" + fileName);
} finally {
if (fileName != null) {
inFS.close();
}
}
}
