[英]Program that computes average of numbers from a file (try/catch blocks involved)
我有一个程序应该允许用户输入一个文件名,如果正确的话,它会计算文件中数字的平均值并将其打印出来。 同样,如果用户多次输入错误的文件名,程序就会退出。 在程序中,“try”块应该实例化 Scanner 并将 fileOk 设置为 true,而“catch”块应该提示用户重新输入文件名、读取文件并递增 fileTry。 第一个“if”语句也应该如此,以便计算平均值或将错误消息写入 output 文件 (outNumbers.dat)。 这是我到目前为止的代码:
package average;
import java.io.*;
import java.util.Scanner;
public class Try
{
static Scanner inFile;
public static void main(String[] args) throws IOException
{
int fileTry = 0;
String fileName;
Scanner inName = new Scanner(System.in);
System.out.println("Enter file name>");
fileName = inName.nextLine();
boolean fileOk;
do
{
fileOk = false;
try
{
Scanner file = new Scanner(new File(fileName));
fileOk = true;
}
catch(FileNotFoundException error)
{
System.out.println("Reenter file name>");
fileName = inFile.nextLine();
fileTry++;
}
} while (!fileOk && fileTry < 4);
PrintWriter outFile = new PrintWriter(new FileWriter("outNumbers.dat"));
if (fileName != null )
{
int numDays = 0;
double average;
double inches = 0.0;
double total = 0.0;
while (inFile.hasNextFloat())
{
inches = inFile.nextFloat();
total = total + inches;
outFile.println(inches);
numDays++;
}
if (numDays == 0)
System.out.println("Average cannot be computed " +
" for 0 days.");
else
{
average = total / numDays;
outFile.println("The average rainfall over " +
numDays + " days is " + average);
}
inFile.close();
}
else
System.out.println("Error");
outFile.close();
}
}
这是正确文件(inNumbers.dat)的内容:
2.3
3.1
0.3
1.1
2.2
2.1
0.0
0.4
0.76
0.5
1.0
0.5
这是运行程序并输入正确的文件名后得到的 output:
Enter file name>
inNumbers.dat
Exception in thread "main" java.lang.NullPointerException
at average.Try.main(Try.java:40)
我对如何解决这个问题有点迷茫:/
NullPointerException 在 java 中很常见。这通常意味着您正在尝试使用从未初始化过的 object。 在这种情况下, infile
仅被声明而从未被初始化。
Scanner file = new Scanner(new File(fileName));
file
变量只存在于 try 块中,因为这是它被声明的地方。 您可能想改为初始化infile
。
infile = new Scanner(new File(fileName));
我有一个程序应该允许用户输入文件名,如果正确,它会计算文件中数字的平均值并将其打印出来。 同样,如果用户输入错误的文件名一定次数,则程序退出。 在程序中,“try”块应该实例化Scanner并将fileOk设置为true,而“catch”块应该提示用户重新输入文件名,读取文件,并增加fileTry。 第一个“if”语句也应该用来计算平均值或在 output 文件 (outNumbers.dat) 上写入错误消息。 这是我到目前为止的代码:
package average;
import java.io.*;
import java.util.Scanner;
public class Try
{
static Scanner inFile;
public static void main(String[] args) throws IOException
{
int fileTry = 0;
String fileName;
Scanner inName = new Scanner(System.in);
System.out.println("Enter file name>");
fileName = inName.nextLine();
boolean fileOk;
do
{
fileOk = false;
try
{
Scanner file = new Scanner(new File(fileName));
fileOk = true;
}
catch(FileNotFoundException error)
{
System.out.println("Reenter file name>");
fileName = inFile.nextLine();
fileTry++;
}
} while (!fileOk && fileTry < 4);
PrintWriter outFile = new PrintWriter(new FileWriter("outNumbers.dat"));
if (fileName != null )
{
int numDays = 0;
double average;
double inches = 0.0;
double total = 0.0;
while (inFile.hasNextFloat())
{
inches = inFile.nextFloat();
total = total + inches;
outFile.println(inches);
numDays++;
}
if (numDays == 0)
System.out.println("Average cannot be computed " +
" for 0 days.");
else
{
average = total / numDays;
outFile.println("The average rainfall over " +
numDays + " days is " + average);
}
inFile.close();
}
else
System.out.println("Error");
outFile.close();
}
}
这是正确文件的内容(inNumbers.dat):
2.3
3.1
0.3
1.1
2.2
2.1
0.0
0.4
0.76
0.5
1.0
0.5
这是我在运行程序并输入正确的文件名后得到的 output:
Enter file name>
inNumbers.dat
Exception in thread "main" java.lang.NullPointerException
at average.Try.main(Try.java:40)
我对如何解决这个问题有点迷茫:/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.