2016-04-26 96 views
0

所以我正在做一个CS课程的作业,涉及使用hashmaps,我需要跟踪一个单词出现在页面上的次数。哈希值中的值乘以15?

现在出于一些奇怪的原因,我遇到了最奇怪的错误,一切似乎正常工作,一切都存储在地图中,但是,当我打印出来测试时,地图中的每个值乘以15.

成员仅出现8次,但我的输出是120

雷丁仅出现32次,但我的输出是480

下面是相关的代码:

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); 

} 

有没有人知道这里会发生什么?

编辑:于正则表达式和这样的代码:

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

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

如果问题出在这里比我不明白为什么,因为这符合我的老师的示例代码,和他的样品运行不有这个问题。

+1

你的正则表达式匹配太多了吗? –

+0

相关代码不在您发布的代码中。看到上面的评论... –

+0

嗯,这看起来[非常熟悉](http://stackoverflow.com/questions/36879295/sorting-maps-without-a-comparator-an-arraylist-or-a-treeset/36879435 #comment61324680_36879295)... – shmosel

回答

0

当使用match.group()时,您并不是只抓取所需匹配的单个实例。

考虑在第一个循环中输出检查的值,看看那里发生了什么。

+0

谢谢,这指出我在正确的方向,我想我知道如何解决这个问题。 – InstantRegret