我一直在做的這段代碼假設每當代碼在文件中找到一個術語時就添加一個計數器。計數器表示包含該術語的文檔數量。計數器不起作用
System.out.println("Please enter the required word :");
Scanner scan2 = new Scanner(System.in);
String word2 = scan.nextLine();
String[] array2 = word2.split(" ");
for (int b = 0; b < array.length; b++) {
for (int i = 0; i < filename; i++) {
try {
BufferedReader in = new BufferedReader(new FileReader(
"C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
+ i + ".txt"));
int numDoc = 0;
int numofDoc = 0;
Scanner s2 = new Scanner(in);
{
while (s2.hasNext()) {
if (s2.next().equals(word2))
numDoc++;
}
}
if (numDoc > 0)
numofDoc++;
System.out.println("File containing the term is "
+ 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。
心指出我的錯誤?感謝..
雖然你已經取得了很大的意見,他們AREN」要解決他的問題,所以他們不屬於答案。 – Gabe 2011-03-09 11:07:54
謝謝你的觀點。當我從第二個for循環獲取println後,numofDoc變量將被初始化。有什麼建議?謝謝你借給我你的時間。 – 2011-03-09 11:15:24
@加貝這是家庭作業,我認爲#3通過一點思考給解決問題帶來了很好的線索。沒有用於提供現成的解決方案。 – 2011-03-09 11:33:47