I am attempting to extract words as strings out of one very long string that contains many words. I’m trying to take this big blob of a string, move through it with a for loop, building strings as I go, adding them to the hashset, and keeping track of how many words I have in the process. I was so proud out myself when I had everything built and eclipse wasn’t showing any obvious errors, then when I went to test it I had a 0 counter, 0 hashset.size and an empty hashset 🙁
Here is the code I’ve been fudging with:
public int countUniqueWords(String line) {
hashset = new HashSet<String>();
word = new StringBuilder();
int endOfLine = line.length() - 1;
boolean isWord = false;
String stringWord = null;
Integer counter = 0;
for (int i = 0; i < line.length(); i++) {
if (Character.isLetter(line.charAt(i)) == true && i != endOfLine) {
word.append(line.charAt(i));
} else if (Character.isLetter(line.charAt(i)) == false && isWord == true) {
counter++;
stringWord = word.toString();
hashset.add(stringWord);
word = null;
isWord = false;
} else if (Character.isLetter(line.charAt(i)) && i == endOfLine) {
counter++;
stringWord = word.toString();
hashset.add(stringWord);
}
}
System.out.println(counter);
System.out.println(hashset.size());
System.out.println(hashset);
return counter;
}
I will continue to search and kick the tires. In the meantime, if anyone has any suggestions I will reward you with lots of mental good vibes over here in my little home office. It seems like there are at least one or two very fundamental errors I am making here, because obviously it’s not even iterating through the loop like it’s supposed to. I suspect it has something to do with me using StringBuilder together with HashSets and the Character class, all mashed together while not understanding any of these particularly well. And yes, I’ve poured over the oracle docs.
The first condition is true for all characters
The second condition is always false as isWord is false, the statements under condition will never execute
The third condition will never execute as this is same as first condition
You need to change the conditions by turning on/off the ‘isWord’ flag when required.