저는 학교 과제를 위해 일하고 있습니다. 이 프로그램은 사용자에게 파일 이름을 묻고 해당 파일의 문자, 단어 및 줄 수를 계산합니다. 그런 다음 다음 파일의 이름을 묻습니다. 사용자가 존재하지 않는 파일을 입력하면 프로그램은 처리 된 모든 파일에 문자, 단어 및 행의 총 수를 인쇄하고 존재합니다.변수가 로컬에서 읽을 수 없습니다. 이유를 알아낼 수 없습니다
그래서 프로그램을 작성했지만 몇 가지 오류가 나타납니다. 여기에 나는
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());
}
}
@ PM77-1 덕분에 오류를 표시하기 위해 내 게시물을 편집했습니다. 나는 당신이 논평하는 동안 편집 중이었습니다. 너희들은 너무 빠르다 :-) –