【问题标题】:Counting the number of times each element of an array occurs in a file计算数组中每个元素在文件中出现的次数
【发布时间】:2017-08-28 07:51:29
【问题描述】:

我写了下面的代码来回答上面的问题。谁能告诉我哪里出错了。

我希望看到代码返回数组中每个元素在文本文件中出现的确切次数。不考虑空格、制表符、换行符等。

public class counter {
    public static void main(String[] args) throws FileNotFoundException  {
        String[] wordname;
        wordname = new String[] {"harry","ron","george","fred"};
        File file = new File("file.txt");
        Scanner scanner = new Scanner(file);
        for(int i=0; i < wordname.length; i++){
            scanner.useDelimiter(wordname[i]);
            int occurences = 0;
            while(scanner.hasNext()){
                scanner.next();
                occurences++;

            }
            System.out.println(wordname[i] + ": " + occurences);
        }
        scanner.close();

    }
}

输出:
哈利:6
罗恩:1
乔治:0
弗雷德:0

文件:
哈利·哈利·罗恩·乔治·哈利·哈利 harry harry har
ron ron ron ron Fred 弗雷德弗雷德乔治 哈利

【问题讨论】:

  • 你的输出是什么?能给我举个例子吗?似乎它可能只经历一次。
  • 同时显示文本文件中的内容。
  • 输出:harry:6 ron:1 george:0 fred:0 文件:harry harry ron george harry harry harry harry har ron ron ron fred fred fred george harry
  • 正如您的代码一样,您正在尝试读取每个单词的整个文件。在这种情况下,您应该在 for 循环内初始化并关闭 Scanner,而不是在外部。
  • 糟糕的是,我初始化了它并且它有效!谢谢:)

标签: java arrays java.util.scanner java-io


【解决方案1】:

您应该使用 White Space 而不是 word 和您的代码扫描文件进行多次拆分,这也会降低性能。

所以最好以这种方式执行相同的任务:

public class Counter {
public static void main(String[] args) throws FileNotFoundException {
    String[] wordname;
    wordname = new String[]{"harry", "ron", "george", "fred"};
    Map<String, Integer> map = new HashMap<>();
    for (String string : wordname) {
        map.put(string, 0);
    }
    File file = new File("file.txt");
    Scanner scanner = new Scanner(file);
    scanner.useDelimiter("\\s+");
    String word;
    while (scanner.hasNext()) {
        word = scanner.next();
        if (map.containsKey(word)) {
            map.put(word, map.get(word) + 1);
        }
    }

    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        System.out.println(entry.getKey() + ":" + entry.getValue());
    }
}
}

【讨论】:

    【解决方案2】:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Example {
        public static void main(String[] args) throws FileNotFoundException {
            //read whole file to string only once
            String fileContent = new Scanner(new File("file.txt")).useDelimiter("\\Z").next();
            String[] wordname = {"harry","ron","george","fred"};
            // filter each name and count occurrences
            for (String name : wordname){
                long count = Arrays.stream(fileContent.split(" ")).filter(s -> s.equals(name)).count();
                System.out.println(name + " : " + count);
            } 
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 2022-11-30
      • 1970-01-01
      相关资源
      最近更新 更多