[英]NumberFormatException when reading from file sequentially?
該程序的主要目的是將學生信息寫入文件並從同一文件讀取。 該程序包括一個if語句,以告知學生是否處於良好狀態或學術試用期,並分別提供相應的文件(goodstanding.txt和probation.txt)。 如果我注釋掉goodstanding.txt的讀取邏輯,該程序將運行文件,但對我來說,除了文件路徑外,它們似乎幾乎完全相同。 我瀏覽了其他問題,但其中大多數似乎與錯誤的類型轉換有關。 它引發的特定錯誤是NumberFormatException For input string: ""
行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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.