1
// Calculating term frequency
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");
}
}
이것은 텍스트가 포함 된 텍스트 파일의 단어 수, 총 단어 및 용어 빈도를 계산하는 코드입니다. 문제는 내가 for 루프 외부 변수 wordcount 및 totalcount에 액세스해야합니다. 그러나 wordcount 및 totalcount 변수를 선언 할 위치를 변경하면 결과가 너무 정확하지 않게 변경됩니다. 누군가가 정확한 결과를 얻고 for 루프 외부의 변수에 액세스 할 수 있도록 변수에 대한 도움을 줄 수 있습니까?변수 문제