无法调用某些变量

时间:2011-03-11 14:48:06

标签: java

//Calculating term frequency
    System.out.println("The number of files is this folder is : " + numDoc);

    System.out.println("Please enter the required word  :");
    Scanner scan = new Scanner(System.in);
    String word = scan.nextLine();

    String[] array = word.split(" ");
    int filename = 11;
    String[] fileName = new String[filename];
    int a = 0;

    for (a = 0; a < filename; a++) {
        try {
            System.out.println("The word inputted is " + word);
            File file = new File(
                    "C:\\Users\\user\\fypworkspace\\TextRenderer\\abc" + a
                            + ".txt");
            System.out.println(" _________________");

            System.out.print("| File = abc" + a + ".txt | \t\t \n");

            for (int i = 0; i < array.length; i++) {

                int totalCount = 0;
                int wordCount = 0;

                Scanner s = new Scanner(file);
                {
                    while (s.hasNext()) {
                        totalCount++;
                        if (s.next().equals(array[i]))
                            wordCount++;

                    }

                    System.out.print(array[i] + " ---> Word count =  "
                            + "\t\t " + "|" + wordCount + "|");
                    System.out.print("  Total count = " + "\t\t " + "|"
                            + totalCount + "|");
                    System.out.printf("  Term Frequency =  | %8.4f |",
                            (double) wordCount / totalCount);

                    System.out.println("\t ");

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

        }

    }
// Count inverse document frequency

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

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



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

            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().equals(array2[b]))
                            matchedWord++;
                    }

                }
                if (matchedWord > 0)
                    numofDoc++;

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

        }
        System.out.println(array2[b] + " --> This number of files that contain the term  " +  numofDoc);
        double inverseTF = Math.log10 ( (float)numDoc/ numofDoc );
        System.out.println(array2[b] + " --> IDF " +  inverseTF);
        double TFIDF = ((double) wordCount / totalCount)) * inverseTF);
    }

}

我无法计算TFIDF,因为编译器说wordCount没有初始化为变量。我无法从上面的代码中调用它。任何指导?谢谢。

4 个答案:

答案 0 :(得分:4)

wordCount是在for循环中声明的局部变量。一旦循环结束,它就会超出范围而无法使用。同样是totalCount的问题。把它放在for循环之前,而不是

int wordCount = 0;
int totalCount = 0;
for (a = 0; a < filename; a++) {
   // ....
}

答案 1 :(得分:1)

       for (int i = 0; i < array.length; i++) {
            int totalCount = 0;
            int wordCount = 0;

这定义了for循环范围内的totalCount和wordCount。您正尝试从for循环外部访问这些变量(下方)。您可以做的更多是这些声明到顶部,例如在哪里写String word = scan.nextLine();

答案 2 :(得分:1)

因为您在无法到达的位置初始化wordCount变量 double TFIDF = ((double) wordCount / totalCount)) * inverseTF);

答案 3 :(得分:0)

wordCount在for循环中定义,并且您尝试访问所述循环之外的变量,这不起作用。

您必须将变量定义移动到其他位置,例如在方法的开头。