0

So I'm working on an assignment for a CS course involving the use of hashmaps where I need to keep track of the number of times that a word appears on the page.

Now for some strange reason, I've run into the weirdest bug, where everything seems to work properly and everything is stored in the map, however, when I print out to test, every value in the map is multiplied by 15.

members only appears 8 times, but my output is 120.

redding only appears 32 times, but my output is 480.

Here is the relevant code:

Map<String, Integer> found = new HashMap<>();

while (match.find()) {
    String word = match.group().toLowerCase();
    Integer check = found.get(word);

    if (check == null) {
        found.put(word, 1);
    } else {
        found.put(word, check+1);
    }
}
...
for (Map.Entry<String, Integer> entry : found.entrySet()) {
    String k = entry.getKey();
    Integer i = entry.getValue();

    System.out.println("Word: " + k + " \t\t\tFrequency: " + i);

}

Does anybody have an idea of what might be going on here?

EDIT: The code for the regex and such:

String word_pattern = "[A-Za-z]{5,}";
String content = WebDoc.getBodyContent(url);

Matcher match = Pattern.compile(word_pattern).matcher(content);

If the problem is in here than I don't understand why, as this matches my instructor's sample code, and his sample run doesn't have this problem.

1 Answers1

0

When using match.group() you're not grabbing only a single instance of that match you want.

Consider outputting the values of check in your first loop and see what's going on there.