So I'm writing a function parse() to read through a specified file line by line, and I'm utilizing the String.split(regex) function to break the line into a string array of words. Additionally, each word will be passed to a HashMap contained in another function passed as an argument to parse(). While I have browsed Stack Overflow for anything to help, I'm still not sure why my function is causing my program to hang.
Here is the function itself:
public void parse(FrequencyCounter counter) throws IOException {
BufferedReader fileReader = new BufferedReader(new FileReader(file));
String searchExpression = "[\\p{Space}\\p{Punct}]";
String line;
String[] wordList;
line = fileReader.readLine();
while (!line.isEmpty()) {
wordList = line.split(searchExpression);
System.out.println("First value of wordList: " + wordList[0]);
for (String each : wordList) {
if(each.isEmpty())
break;
if(counter.isAnElement(each)) {
counter.incrementKey(each);
} else {
counter.addKey(each);
}
}
}
fileReader.close();
}
So I can get into the while loop just fine, but when add a println() statement after the supposed construction of the wordList, the program infinitely prints out an empty wordList and the program does not return from the function. From my perspective, I am led to believe that the regular expression I am using does not achieve the ends I desire.
To elaborate on what I need, the regular expression should isolate words made up of only alphabetical characters. In the perfect case, conjunctions or hyphenated words could be recognized in their entirety and added to the wordList. However, I can accept words like "won't" and "twenty-two" to become "won", "t", "twenty", and "two".
As a test case, I am running a plain-text file containing the poem "Jabberwocky" by Lewis Carroll, though this does not contain very difficult non-word tokens to split around.
What is it that causes this issue, and how can I improve the parsing done by this function?
You do not alter your string line inside the while loop, so !line.isEmpty() will never be met.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With