变量未读取本地错误。无法弄清楚为什么

时间:2013-09-01 15:56:58

标签: java

我正在完成一项学校作业。程序会询问用户文件名,并计算该文件中的字符数,单词数和行数。然后询问下一个文件的名称。当用户输入不存在的文件时,程序将打印所有已处理文件中的字符,单词和行的总计数并存在。

所以我编写了程序,但是我遇到了一些错误。这是计数器程序,我在线上得到错误

private FileCounter counter; 

private boolean done;

错误说明: FieldCounter.done永远不会在本地阅读。前一行也是如此。我无法弄清楚为什么我会收到这个警告。

该计划的其余部分:

    import java.util.Scanner;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    /**
    * A class to count the number of characters, words, and lines in files.
    */

    public class FileCounter
    {
       /**
          Constructs a FileCounter object.
       */
       public FileCounter()
       {
        words = 0;
        lines = 0;
        chars = 0;
        input = "";

     }

       /**
          Processes an input source and adds its character, word, and line
          counts to this counter.
          @param in the scanner to process
       */
       public void read(Scanner in) throws FileNotFoundException
       {

           boolean done = false;
           while (!done)
           {
                 while(in.hasNextLine())
                 {
                   lines++;
                   words++;
                   int j = 0;
                   file = in.nextLine();
                   input = input + file;
                   for(int i = 1; i < file.length(); i++)
                   {
                   if(file.substring(j, i).equals(" "))
                   {
                     words++;
                   }


                   j++;

                }
                  }
                   char[] array = input.toCharArray();
                   int num = array.length;
                   chars += num;
                   if(in.hasNextLine() == false)
                   done = true;
                }



      }
      /**
          Gets the number of words in this counter.
          @return the number of words
       */
       public int getWordCount()
       {
         return words;
      }

       /**
          Gets the number of lines in this counter.
          @return the number of lines
       */
       public int getLineCount()
       {    
          return lines;
       }    

       /**
          Gets the number of characters in this counter.
          @return the number of characters
       */
       public int getCharacterCount()
       {

           return chars;
       }

       private String input;
       private int words;
       private FileCounter counter;
       private int lines;
       private boolean done;
       private int chars;
       private String file;

    }


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

/**
   This class prints a report on the contents of a number of files.
*/
public class FileAnalyzer
{
   public static void main(String[] args) 
   {  
      Scanner in = new Scanner(System.in);
      FileCounter counter = new FileCounter();
      boolean more = true;
      while (more)
      {
         System.out.print("Please enter the next filename, or <Enter> to quit: ");
         String filename = in.nextLine();
         if (filename.length() > 0)
         {
             try
             {
                 FileReader fileRead = new FileReader(filename);
                 Scanner fileInput = new Scanner(fileRead);
                 counter.read(fileInput);
             }
             catch (FileNotFoundException fnfe)
             {
                 System.out.println("File " + filename + " was not found: " + fnfe);
             }
         }
         else
         {
           more = false;
         }
      }
      System.out.println("Characters: " + counter.getCharacterCount());
      System.out.println("Words: " + counter.getWordCount());
      System.out.println("Lines: " + counter.getLineCount());
   }
}

3 个答案:

答案 0 :(得分:1)

您获得的警告意味着该变量基本上没用,因为它永远不会被读取。删除这些行以及对它们的所有引用,理论上你的程序仍然会运行,不会发生任何变化。

private boolean done;这从未使用过。 private FileCounter counter;等等。

答案 1 :(得分:1)

这不是错误,而是警告。程序编译好了,但发现了一些奇怪的东西。

在这种情况下,您正在定义private布尔属性,但您从未将其用于任何事情(请注意,您在done方法中使用的read是在方法中本地定义的,所以这是一个不同的变量。)

只需删除private boolean done行即可。

同样适用于counter

答案 2 :(得分:1)

您有一个永远不会分配给的私有成员变量done。大概是因为你在done方法中声明了一个局部变量read并使用它。 这是FileCounter.done 的完全独立变量,因此永远不会使用FileCounter.done

变化:

boolean done = false;

简单地说:

done = false;

您不再创建单独的局部变量,而是使用类'成员变量。

或者,移除private boolean done;以移除成员变量,但是 - 如果您这样做 - 请记住done是一个局部变量,不会保留其值跨多个调用read(在这种情况下可能是你想要的,但了解区别很重要。)

相关问题