【发布时间】:2015-03-26 20:20:23
【问题描述】:
我正在编写一个程序来分析输入的文本并计算平均字母数。 我让它在用户输入文本时工作,但我无法让它在从文件中读取文本时工作。
public static void ReadFromFile() throws IOException {
String filename = "name.txt";
String str = new String(new char[100]);
try{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while ((str = br.readLine()) !=null) {
System.out.println(str + "\n");
}
br.close();
} catch(IOException e) {
System.out.println("File not found");
}
while (c != str.length()) {
if (str.charAt(c) >= 'a' && str.charAt(c) <= 'z') {
count[str.charAt(c) - 'a']++;
}
c++;
}
for (c = 0; c<26;c++){
if (count[c] !=0){
double num = count[c];
double denom = str.length();
double average = num / denom;
System.out.print((char) (c + 'a') + " occurs " + count[c] + " times in the entered string. Average is ");
System.out.println(average);
}}}}
我不断收到一个错误提示
Exception in thread "main" java.lang.NullPointerException
at MainMenu.ReadFromFile(MainMenu.java:79)
at MainMenu.main(MainMenu.java:25)
任何帮助将不胜感激 谢谢
【问题讨论】:
-
第 79 行是“while (c != str.length()) {”
标签: java arrays file console ioexception