文本 IO 流编号格式异常

Text IO Stream Number Format Exception

我一直在 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 循环时,我得到了一些值,然后是错误,然后是所有其余值,就好像没有问题一样。我注意到,当我删除并重新输入错误开始处的值时,错误会移动到其他地方。我检查了 rent class 中的参数(它需要 6 个整数)并检查了所有值都正确的 .txt 文件。 我该如何解决这个问题,或者我不应该担心它并添加另一个 catch 语句来忽略错误?

您正在用 != 比较字符串。您应该使用 !line.equals("")。 – 救世主自己