在此 Java 代码中将 Java 中的两种方法组合在一起

combine two methods in Java together in this Java code

我想结合这两种方法 我的文档解析器 frequencyCounter 和 parseFiles 代码中只有一些错误。

我希望所有的 frequencyCounter 应该是一个应该从 parseFiles 中执行的函数,并且不用担心文件内容的相关信息应该传递给 doSomething 以便它知道要打印什么。

最近一直在纠结怎么把这两种方法结合起来,请大家指点一下

这是我的主class:

public class Yolo {
    public static void frodo() throws Exception {
        int n; // number of keywords
        Scanner sc = new Scanner(System.in);
        System.out.println("number of keywords : ");
        n = sc.nextInt();
        for (int j = 0; j <= n; j++) {

            Scanner scan = new Scanner(System.in);
            System.out.println("give the testword : ");
            String testWord = scan.next();
            System.out.println(testWord);

            File document = new File("path//to//doc1.txt");
            boolean check = true;

            try {
                FileInputStream fstream = new FileInputStream(document);
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine;
                strLine = br.readLine();

                // Read File Line By Line

                int count = 0;
                while ((strLine = br.readLine()) != null) {

                    // check to see whether testWord occurs at least once in the
                    // line of text
                    check = strLine.toLowerCase().contains(testWord.toLowerCase());

                    if (check) {
                        // get the line
                        String[] lineWords = strLine.split("\s+");
                        // System.out.println(strLine);
                        count++;
                    }

                }
                System.out.println(testWord + "frequency: " + count);

                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

下面的代码给你这个输出:

Professor frequency: 54
engineering frequency: 188
data frequency: 2
mining frequency: 2
research frequency: 9

虽然这仅适用于 doc1,但您必须添加一个循环来迭代所有 5 个文档。

public class yolo {
    public static void frodo() throws Exception {

        String[] keywords = { "Professor" , "engineering" , "data" , "mining" , "research"};
        for(int i=0; i< keywords.length; i++){

        String testWord = keywords[i];
        File document = new File("path//to//doc1.txt");
        boolean check = true;

        try {
            FileInputStream fstream = new FileInputStream(document);
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
            strLine = br.readLine();

            // Read File Line By Line

            int count = 0;
            while ((strLine = br.readLine()) != null) {

                // check to see whether testWord occurs at least once in the
                // line of text
                check = strLine.toLowerCase().contains(testWord.toLowerCase());

                if (check) {
                    // get the line
                    String[] lineWords = strLine.split("\s+");
                    count++;
                }

            }
            System.out.println(testWord + "frequency: " + count);

            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

}

希望对您有所帮助!