首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >containsKey方法的TreeMap返回假,尽管键已经在地图上

containsKey方法的TreeMap返回假,尽管键已经在地图上
EN

Stack Overflow用户
提问于 2017-06-22 13:34:06
回答 1查看 318关注 0票数 0

我试图编写一个计算文本文件中所有单词的程序。我在TreeMap中放置了与模式匹配的任何单词。

我通过args0获得的文本文件

例如,文本文件包含以下文本:The Project Gutenberg EBook of The Complete Works of William Shakespeare

检查TreeMap是否已经有单词的条件,返回false作为word The的第二个外观,但返回true单词of的第二个外观。

我不明白为什么..。

这是我的密码:

代码语言:javascript
复制
public class WordCount
{
    public static void main(String[] args)
    {
        // Charset charset = Charset.forName("UTF-8");
        // Locale locale = new Locale("en", "US");

        Path p0 = Paths.get(args[0]);
        Path p1 = Paths.get(args[1]);
        Path p2 = Paths.get(args[2]);

        Pattern pattern1 = Pattern.compile("[a-zA-Z]");
        Matcher matcher;
        Pattern pattern2 = Pattern.compile("'.");

        Map<String, Integer> alphabetical = new TreeMap<String, Integer>();

        try (BufferedReader reader = Files.newBufferedReader(p0))
        {
            String line = null;

            while ((line = reader.readLine()) != null)
            {
                // System.out.println(line);
                for (String word : line.split("\\s"))
                {
                    boolean found = false;

                    matcher = pattern1.matcher(word);
                    while (matcher.find())
                    {
                        found = true;
                    }
                    if (found)
                    {
                        boolean check = alphabetical.containsKey(word.toLowerCase());
                        if (!alphabetical.containsKey(word.toLowerCase()))
                            alphabetical.put(word.toLowerCase(), 1);
                        else
                            alphabetical.put(word.toLowerCase(), alphabetical.get(word.toLowerCase()).intValue() + 1);
                    }
                    else
                    {
                        matcher = pattern2.matcher(word);
                        while (matcher.find())
                        {
                            found = true;
                        }
                        if (found)
                        {
                            if (!alphabetical.containsKey(word.substring(1, word.length())))
                                alphabetical.put(word.substring(1, word.length()).toLowerCase(), 1);
                            else
                                alphabetical.put(word.substring(1, word.length()).toLowerCase(), alphabetical.get(word).intValue() + 1);
                        }
                    }
                }
            }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-22 14:24:42

我已经测试过你的代码了,没关系。我想你得检查一下你的文件编码。

这当然是在"UTF-8“中。把它放在“没有BOM的UTF-8”中,你就会没事的!

编辑:如果你不能改变编码,你可以手动完成。请参阅此链接:http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html

问候

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44700956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档