2011-03-09 78 views
0

我一直在做的這段代碼假設每當代碼在文件中找到一個術語時就添加一個計數器。計數器表示包含該術語的文檔數量。計數器不起作用

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。

心指出我的錯誤?感謝..

回答

3
  1. 縮進你的代碼正確(Eclipse中下,按Ctrl + Shift + F會爲你做它)
  2. 給出有意義的,明確的名稱,以您的變量。 numDoc和numOfDoc太接近以至於不會出錯
  3. 您正在內循環中輸出計數器,嘗試從第二個for循環中取出System.out.println("File containing the term is " + numofDoc);(如果您正確縮進代碼,可以很容易地發現該循環)。同時檢查你是否正在輸出正確的變量。

現在您打印結果在適當的位置,int numofDoc = 0;也應當第二for循環外。

此外,您正在使用String.equals來檢查文件的當前行是否包含所需的文本。也許你想尋找的文件String.contains

+0

雖然你已經取得了很大的意見,他們AREN」要解決他的問題,所以他們不屬於答案。 – Gabe 2011-03-09 11:07:54

+0

謝謝你的觀點。當我從第二個for循環獲取println後,numofDoc變量將被初始化。有什麼建議?謝謝你借給我你的時間。 – 2011-03-09 11:15:24

+0

@加貝這是家庭作業,我認爲#3通過一點思考給解決問題帶來了很好的線索。沒有用於提供現成的解決方案。 – 2011-03-09 11:33:47

0

我想numDoc代表文件中出現的次數和numofDoc表示文件的數量。

問題是在for循環中設置了變量int numofDoc = 0。因此,對於每個新文件,計數器都會重置。

0

Set int numDoc = 0;在兩個循環之前。

因此,您在每次執行循環時都將值設回爲0。

0

declare int numDoc = 0; int numofDoc = 0; outside for循環。每當執行爲循環 ,他們初始化&進行增量爲1。這就是爲什麼你讓所有的時間1.

0

我想你想這樣做

public static void main(String[] args) 
{ 
    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++) 
    { 
     **//Declare before the loop** 
     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(word2)) 
          matchedWord++; 
        } 


       } 
       if (matchedWord > 0) 
        numofDoc++; 
       System.out.println("File containing the term is " + numofDoc); 

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

是的,但我想打印輸出單行顯示 含有該術語的文件是3. 這意味着它已經總結了總數。並且只在輸出中打印出一行。任何想法先生? – 2011-03-09 12:22:29