我一直在java上编程Monopoly,当我从一个NumberFormatException文件中提取租金值时,我遇到了一个.txt。
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循环的注释时,我会得到一些值,后面跟着错误,然后所有其余的值都好像没有问题一样。我注意到,当我删除并重新输入错误开始的值时,错误会转移到其他地方。我检查了rent类中的参数(它需要6个int's),并检查了所有值都很好的.txt文件。我如何解决这个问题,或者我不应该担心它,并添加另一个catch语句来忽略错误?
发布于 2015-11-09 17:05:10
你在比较字符串和!=。您应该使用!line.equals("")。-救世主的自我
https://stackoverflow.com/questions/33572780
复制相似问题