我正在做一个学校作业。该程序要求用户输入文件名并计算该文件中的字符数、单词数和行数。然后询问下一个文件的名称。当用户输入不存在的文件时,程序会打印所有已处理文件中的字符、单词和行的总计数并且存在。
所以我编写了程序,但我遇到了一些错误。这是计数器程序,我在线收到错误
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());
}
}