我一直在使用java编写Monopoly,当我从.txt文件中提取租金值时,我遇到了NumberFormatException。
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
以下是代码:
try{
//Searching for .txt file
fileName = "N://java/Monopoly/src/rent.txt";
//creating FileReader and BuffReader objects
FileReader input = new FileReader(fileName);
BufferedReader bufRead = new BufferedReader(input);
//reading first line
String line = bufRead.readLine();
while(line!=""){
//not sure why, but program doesn't run well without if statement
if(line!=""){
//creating array of in variable
splitArr = line.split(",");
//creating rent object
rent r = new rent(Integer.parseInt(splitArr[0]),Integer.parseInt(splitArr[1]),Integer.parseInt(splitArr[2]),Integer.parseInt(splitArr[3]),Integer.parseInt(splitArr[4]),Integer.parseInt(splitArr[5]));
//storing rent object to a public static Arraylist holding all rents for the game
rents.add(r);
//debugging code that has been commented out
//System.out.println(r.toString());
}
//debugging code that has been commented out
/*for(String s : splitArr){
System.out.print(s+", ");
}*/
//reading next line
line = bufRead.readLine();
}
//closing IO stream
bufRead.close();
//preventing out of bounds exception error
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("Usage: java ReadFile filename: rent\n");
//preventing IOException error
}catch (IOException e){
e.printStackTrace();
//I can't quite remember I have this in there. I know it didn't work right without it at some point
}catch(NullPointerException e){
}
当我在第16行取消注释增强的for循环时,我得到一些值,然后是错误,然后是所有其余值,就好像没有问题一样。我注意到当我删除并重新输入错误开始的值时,错误会移动到其他位置。我已经检查了租借类中的参数(它需要6个int),并检查了所有值都很好的.txt文件。 我该如何解决这个问题,或者我不应该担心它并添加另一个catch语句来忽略错误?
答案 0 :(得分:0)
您正在将字符串与!=进行比较。你应该使用!line.equals("")。 - 救主自我