gpt4 book ai didi

java - 变量未在本地读取错误。不明白为什么

转载 作者:行者123 更新时间:2023-12-01 23:36:43 24 4
gpt4 key购买 nike

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

所以我编写了该程序,但出现了一些错误。这是计数器程序,我在线收到错误

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());
}
}

最佳答案

您有一个私有(private)成员变量done,它从未被赋值。大概是因为您在 read 方法中声明了一个局部变量 done 并使用了它。 这是一个与 FileCounter.done 完全独立的变量,因此永远不会使用 FileCounter.done

更改:

boolean done = false;

简单地说:

done = false;

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

或者,删除 private boolean done; 以删除成员变量,但是 - 如果您这样做 - 请记住 done 是一个局部变量,并且不会在多次调用 read 时保留其值(在本例中这可能是您想要的,但理解其中的区别很重要)。

关于java - 变量未在本地读取错误。不明白为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com