首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文本IO流号格式异常

文本IO流号格式异常
EN

Stack Overflow用户
提问于 2015-11-06 17:54:45
回答 1查看 65关注 0票数 0

我一直在java上编程Monopoly,当我从一个NumberFormatException文件中提取租金值时,我遇到了一个.txt。

代码语言:javascript
复制
 Exception in thread "main" java.lang.NumberFormatException: For input string: ""

下面是代码:

代码语言:javascript
复制
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语句来忽略错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-09 17:05:10

你在比较字符串和!=。您应该使用!line.equals("")。-救世主的自我

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33572780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档