按顺序从文件读取时出现 NumberFormatException?
NumberFormatException when reading from file sequentially?
本质上,该程序的目的是将学生信息写入文件并从同一文件中读取。该程序包括一个 if 语句,用于判断学生是否信誉良好或留校察看,每个都有相应的文件(goodstanding.txt 和 probation.txt)。如果我注释掉 goodstanding.txt 的读取逻辑,程序会运行文件,但对我来说,除了文件路径之外,它们似乎几乎相同,显然。我查看了其他问题,但其中大部分似乎与错误的类型转换有关。它抛出的具体错误是 NumberFormatException For input string: ""
on line ID = Integer.parseInt(array[0]);
写入逻辑如下:
if(GPA >= 2.0) //If GPA >= 2.0, write to good standing file. This is
identical to probation writer, but causes an issue somewhere
{
String result = ID + "," + fname + " " + lname + "," + GPA;
OutputStream out = new BufferedOutputStream(Files.newOutputStream(good,
StandardOpenOption.CREATE));
BufferedWriter wt = new BufferedWriter(new OutputStreamWriter(out));
wt.write(result, 0, result.length()); //Write to file from position 0 to
length
wt.newLine();
System.out.println("Please enter next WIN or 999 to quit: ");
ID = input.nextInt();
input.nextLine();
wt.close();
}
if(GPA < 2.0) //If GPA < 2.0, write to probation file {
String result = ID + "," + fname + " " + lname + "," + GPA;
OutputStream out = new
BufferedOutputStream(Files.newOutputStream(probation,
StandardOpenOption.CREATE));
BufferedWriter wt = new BufferedWriter(new OutputStreamWriter(out));
wt.write(result, 0, result.length());
wt.newLine();
System.out.println("Please enter next WIN or 999 to quit: ");
ID = input.nextInt();
input.nextLine();
wt.close();
}
以及读取逻辑:
try
{
while(line != null)
{
array = line.split(",");
ID = Integer.parseInt(array[0]);
name = array[1];
GPA = Double.parseDouble(array[2]);
double ahead = GPA - 2;
System.out.println("WIN: " + ID + " | " + " Name: " + name + " |" + " GPA
= " + GPA + " | Ahead by " + ahead);
line = reader.readLine();
}
}
catch(NullPointerException e)
{System.out.println("NullPointerException occurred");}
reader.close();
InputStream input2 = new
BufferedInputStream(Files.newInputStream(probation));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(input2));
String line2 = reader2.readLine();
System.out.println();
System.out.println("---------------------------");
System.out.println("--Academic Probation List--");
System.out.println("---------------------------");
try{
while(line2 != null)
{
array = line2.split(",");
ID2 = Integer.parseInt(array[0]);
name2 = array[1];
GPA2 = Double.parseDouble(array[2]);
double fallShort = 2 - GPA2;
System.out.println("WIN: " + ID2 + " | " + " Name: " + name2 + " |" + "
GPA = " + GPA2 + " | Behind by " + fallShort);
line2 = reader2.readLine();
}
}
catch(NullPointerException e)
{e.printStackTrace();}
reader.close();
我也试过在 ID 上使用 trim() 方法,但异常仍然存在。有没有我需要阅读更多的概念来解释这个?
我认为错误消息很清楚 NumberFormatException For input string: ""
因为您无法将空字符串解析为 int。
您的问题至少有两个原因,要么您的文件中有一些空行,要么其中一行以逗号开头。通过执行以下操作来检查或忽略此类行:
....
while (line != null) {
if(!line.startsWith(",") && !line.isEmpty()){
array = line.split(",");
ID = Integer.parseInt(array[0]);
name = array[1];
GPA = Double.parseDouble(array[2]);
double ahead = GPA - 2;
System.out.println("WIN: " + ID + " | " + " Name: " + name + " |" + " GPA = " + GPA + " | Ahead by " + ahead);
}
line = reader.readLine();
}
本质上,该程序的目的是将学生信息写入文件并从同一文件中读取。该程序包括一个 if 语句,用于判断学生是否信誉良好或留校察看,每个都有相应的文件(goodstanding.txt 和 probation.txt)。如果我注释掉 goodstanding.txt 的读取逻辑,程序会运行文件,但对我来说,除了文件路径之外,它们似乎几乎相同,显然。我查看了其他问题,但其中大部分似乎与错误的类型转换有关。它抛出的具体错误是 NumberFormatException For input string: ""
on line ID = Integer.parseInt(array[0]);
写入逻辑如下:
if(GPA >= 2.0) //If GPA >= 2.0, write to good standing file. This is
identical to probation writer, but causes an issue somewhere
{
String result = ID + "," + fname + " " + lname + "," + GPA;
OutputStream out = new BufferedOutputStream(Files.newOutputStream(good,
StandardOpenOption.CREATE));
BufferedWriter wt = new BufferedWriter(new OutputStreamWriter(out));
wt.write(result, 0, result.length()); //Write to file from position 0 to
length
wt.newLine();
System.out.println("Please enter next WIN or 999 to quit: ");
ID = input.nextInt();
input.nextLine();
wt.close();
}
if(GPA < 2.0) //If GPA < 2.0, write to probation file {
String result = ID + "," + fname + " " + lname + "," + GPA;
OutputStream out = new
BufferedOutputStream(Files.newOutputStream(probation,
StandardOpenOption.CREATE));
BufferedWriter wt = new BufferedWriter(new OutputStreamWriter(out));
wt.write(result, 0, result.length());
wt.newLine();
System.out.println("Please enter next WIN or 999 to quit: ");
ID = input.nextInt();
input.nextLine();
wt.close();
}
以及读取逻辑:
try
{
while(line != null)
{
array = line.split(",");
ID = Integer.parseInt(array[0]);
name = array[1];
GPA = Double.parseDouble(array[2]);
double ahead = GPA - 2;
System.out.println("WIN: " + ID + " | " + " Name: " + name + " |" + " GPA
= " + GPA + " | Ahead by " + ahead);
line = reader.readLine();
}
}
catch(NullPointerException e)
{System.out.println("NullPointerException occurred");}
reader.close();
InputStream input2 = new
BufferedInputStream(Files.newInputStream(probation));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(input2));
String line2 = reader2.readLine();
System.out.println();
System.out.println("---------------------------");
System.out.println("--Academic Probation List--");
System.out.println("---------------------------");
try{
while(line2 != null)
{
array = line2.split(",");
ID2 = Integer.parseInt(array[0]);
name2 = array[1];
GPA2 = Double.parseDouble(array[2]);
double fallShort = 2 - GPA2;
System.out.println("WIN: " + ID2 + " | " + " Name: " + name2 + " |" + "
GPA = " + GPA2 + " | Behind by " + fallShort);
line2 = reader2.readLine();
}
}
catch(NullPointerException e)
{e.printStackTrace();}
reader.close();
我也试过在 ID 上使用 trim() 方法,但异常仍然存在。有没有我需要阅读更多的概念来解释这个?
我认为错误消息很清楚 NumberFormatException For input string: ""
因为您无法将空字符串解析为 int。
您的问题至少有两个原因,要么您的文件中有一些空行,要么其中一行以逗号开头。通过执行以下操作来检查或忽略此类行:
....
while (line != null) {
if(!line.startsWith(",") && !line.isEmpty()){
array = line.split(",");
ID = Integer.parseInt(array[0]);
name = array[1];
GPA = Double.parseDouble(array[2]);
double ahead = GPA - 2;
System.out.println("WIN: " + ID + " | " + " Name: " + name + " |" + " GPA = " + GPA + " | Ahead by " + ahead);
}
line = reader.readLine();
}