需要帮助才能理解本指南

时间:2011-03-10 14:45:02

标签: java

本指南由我的一位讲师发表,我想在java中实现。 但我不太了解它如何在java上实现。 有人可以试着为我进一步解释吗?我很欣赏一个很好的教程。 :)

“为文本文件创建一个数组,查看该术语的每个文本文件,一旦获得该术语,您就打破内部循环,更改文件名并再次循环以搜索该术语。然后创建一个计数器计算包含该术语的文本文件的数量。最后,您将从计数器值中获取包含该术语的文档的数量。“

我有一组文本文件,我需要检查文件中是否找到特定术语。如果找到该术语,我需要将fileFound增加到1.

或者更具体地说是逆文档频率。

为了达到上述目的,我需要什么?我的意思是代码透视。对不起,我编码很慢。不,我不希望免费代码。我只想了解指南的内容。我问过我的讲师,但这还不够。我只是想知道如何在java中实现。谢谢你的时间。

System.out.println("Please enter the required word  :");
    Scanner scan2 = new Scanner(System.in);
    String word2 = scan.nextLine();
    String[] array2 = word2.split(" ");

    int numofDoc = 0;
    for (int i = 0; i < filename; i++) {

        for (int b = 0; b < array2.length; b++) {

            try {

                BufferedReader in = new BufferedReader(new FileReader(
                        "C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
                                + i + ".txt"));

                int matchedWord = 0;

                Scanner s2 = new Scanner(in);

                {
                    while (s2.hasNext()) {
                        if (s2.next().contains(word2))
                            matchedWord++;
                    }

                }
                if (matchedWord > 0)
                    numofDoc++;

                System.out.println("This file contain the term  "
                        + numofDoc);

            } catch (IOException e) {
                System.out.println("File not found.");
            }

        }

    }

输出结果为:

请输入所需的字词:

the
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1
File not found
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1

我想输出打印输出单输出,显示包含该项的文件数为10.这意味着它已经总结了总数。并且只在输出中打印一行。任何想法,先生?

4 个答案:

答案 0 :(得分:0)

你的引言没有多大意义。但是,在文件中查找术语的最简单方法是执行

if(FileUtils.readFileToString(filename).contains(term)) 
   counter++;

答案 1 :(得分:0)

我认为这是开始你的工作的充分信息。您的讲师已经详细说明了您需要执行的每个步骤。只需要为它编写代码。

答案 2 :(得分:0)

只有逻辑上的答案是你有一个主循环,你在其中运行不同的文本,并在循环内部运行文本的单词。当找到该术语时使用break;命令停止内循环并增加计数器。所以你需要2个循环,1个数组/文本的arraylist和计数器

答案 3 :(得分:0)

我只是快速浏览一下你的代码,但看起来你的变量'matchedWord'正在计算所有准备好的单词数。您只需要更改定义它的位置,并在方法结束时编写相应的println语句。

相关问题