扫描仪仅适用于多个 类 Java 中的一个

Scanner only works with one out of multiple classes Java

我有一个程序,用于分析用户通过在出现提示时输入文本文件的完整路径来选择的文本文件。

我已经设法让扫描仪进入多个 classes 但是它不会同时对每种方法起作用。例如,我有一个 class 将打印文件中的数字数量,另一个将打印文件中的单词数。但是,只有 运行 的第一种方法有效,另一种方法将显示 class 搜索的任何内容(数字、行、单词等)的 0,即使真实值实际上不是 0。

我真的很不明白为什么会这样,我附上了主要的 class 和另外两个 class 来展示一个清晰的例子:

主要Class:

package cw;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;

public class TextAnalyser {
public static Scanner reader;
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a filename");
        String filename = in.nextLine();
        File InputFile = new File (filename);
        reader = new Scanner (InputFile);


        LineCounter Lineobject = new LineCounter();
        WordCounter Wordobject = new WordCounter();


        Lineobject.TotalLines();
        Wordobject.TotalWords();

    }

}

Class 计算行数:

package cw;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;

public class LineCounter {


    public static void TotalLines() throws IOException {
       // Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
                      Scanner sc = TextAnalyser.reader;

        PrintWriter out = new PrintWriter(new FileWriter("C:\Users\Sam\Desktop\Report.txt", true));
        int linetotal = 0;
        while (sc.hasNextLine()) {
            sc.nextLine();
            linetotal++;
        }
         out.println("The total number of lines in the file = " + linetotal);

             out.flush();

            out.close();
         System.out.println("The total number of lines in the file = " + linetotal);
    }
}

Class 统计字数:

package cw;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.io.IOException;

public class WordCounter {
                     public static  Scanner sc = TextAnalyser.reader;

    public static void TotalWords() throws IOException{
        //Scanner sc = new Scanner(TextAnalyser.class.getResourceAsStream("test.txt"));
        PrintWriter out = new PrintWriter(new FileWriter("C:\Users\Sam\Desktop\Report.txt", true));
        int wordtotal = 0;

  while (sc.hasNext()){
     sc.next();
      wordtotal++;
  }
    out.println("The total number of words in the file = " + wordtotal);
    out.flush();

            out.close();

  System.out.println("The total number of words in the file = " + wordtotal);
}
}

出于某种原因,一次只能工作一个,一个总是说有 0,如果有人能向我解释为什么会发生这种情况以及如何解决它,那将非常有帮助,谢谢!

reader = new Scanner (InputFile); 包含对 Scanner 对象的引用,当您在这两种方法中使用 public static Scanner sc = TextAnalyser.reader; 时,您正在将 reader 的引用复制到 sc,因此所有它们是同一个对象。为什么有 2 个变量引用所有相同的对象,其中一个被重新定义两次并具有相同的值?

这里的问题是扫描器到达文件末尾,当你再次调用它时(它是同一个对象)它没有更多可读的东西,所以你应该创建另一个扫描器对象(也许它是什么你想做什么?)。更好的解决方案是读取一次文件并将内容存储在某种数据结构中。