2011-03-09 66 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