Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Scanner在文件中存储单词的出现次数及其计数。(Java)_Java_Java.util.scanner - Fatal编程技术网

使用Scanner在文件中存储单词的出现次数及其计数。(Java)

使用Scanner在文件中存储单词的出现次数及其计数。(Java),java,java.util.scanner,Java,Java.util.scanner,代码如下: Scanner scan = new Scanner(new FileReader ("C:\\mytext.txt")); HashMap<String, Integer> listOfWords = new HashMap<String, Integer>(); while(scan.hasNextLine()) { Scanner innerScan = new S

代码如下:

        Scanner scan = new Scanner(new FileReader ("C:\\mytext.txt"));
        HashMap<String, Integer> listOfWords = new HashMap<String, Integer>();

        while(scan.hasNextLine())
        {
            Scanner innerScan = new Scanner(scan.nextLine());
            boolean wordExistence ;
            while(wordExistence = innerScan.hasNext())
            {
                String word = innerScan.next(); 
                int countWord = 0;
                if(!listOfWords.containsKey(word)){ already
                    listOfWords.put(word, 1); 
                }else{
                    countWord = listOfWords.get(word) + 1; 
                    listOfWords.remove(word);
                    listOfWords.put(word, countWord); 
                }
            }
        }

        System.out.println(listOfWords.toString());
Scanner scan=新的扫描仪(新的文件阅读器(“C:\\mytext.txt”);
HashMap listOfWords=新HashMap();
while(scan.hasNextLine())
{
Scanner innerScan=新的扫描仪(scan.nextLine());
布尔存在;
while(wordExistence=innerScan.hasNext())
{
String word=innerScan.next();
int countWord=0;
如果(!listOfWords.containsKey(word)){已存在
单词列表。put(单词,1);
}否则{
countWord=listOfWords.get(word)+1;
删除(单词);
单词列表。put(单词,countWord);
}
}
}
System.out.println(listOfWords.toString());
问题是,我的输出包含以下文字:

document.Because=1
document.This=1
space.=1


我如何处理正在发生的句号?(对于进一步的问题,我认为任何句子结束符都是一个问题,如问号或感叹号)。

请查看的课堂笔记,特别是关于使用除空格以外的分隔符的段落。

请查看的课堂笔记,特别是关于使用除空格以外的分隔符的段落。

扫描仪
使用任何空格作为默认分隔符。您可以调用Scanner实例的
useDelimiter()
,并指定您自己的regexp用作分隔符。

Scanner
使用任何空白作为默认分隔符。您可以调用Scanner实例的
useDelimiter()
,并指定您自己的regexp用作分隔符。

如果您希望不仅使用空白分隔符,而且使用
和问号/惊叹号拆分输入,则必须定义一个,然后使用
useDelimiter
().

如果您不仅希望使用空格分隔符,而且希望使用
和问号/感叹号分割输入,则必须定义一个,然后使用
useDelimiter
()()将其应用于扫描仪。

也许您需要修改以下答案以优化速度

    final Pattern WORD = Pattern.compile("\\w+");
    while(scan.hasNextLine())
    {
        Scanner innerScan = new Scanner(scan.nextLine());
        while(innerScan.hasNext(WORD))
        {
            String word = innerScan.next(WORD); 
            if(!listOfWords.containsKey(word)){
                listOfWords.put(word, 1); 
            }else{
                int countWord = listOfWords.get(word) + 1; 
                //listOfWords.remove(word);
                listOfWords.put(word, countWord); 
            }
        }
    }

也许你想修补一下下面的速度优化答案

    final Pattern WORD = Pattern.compile("\\w+");
    while(scan.hasNextLine())
    {
        Scanner innerScan = new Scanner(scan.nextLine());
        while(innerScan.hasNext(WORD))
        {
            String word = innerScan.next(WORD); 
            if(!listOfWords.containsKey(word)){
                listOfWords.put(word, 1); 
            }else{
                int countWord = listOfWords.get(word) + 1; 
                //listOfWords.remove(word);
                listOfWords.put(word, countWord); 
            }
        }
    }