java.lang.NumberFormatException while reading from a file

[亡魂溺海] 提交于 2019-12-13 07:36:03

问题


I'm trying to create a basic program, it reads a file of unknown number of numbers arranged in a matrix, creates a list of arrays in String format to read it and then parses that to int for multiple other processes. I'm getting a java.lang.NumberFormatException when parsing, I know its probably because of a blank value getting parsed to an int. I have looked at other questions but can't seem to get it fixed. Here's part of the code:

public static void main(String[] args) {
    try {
            br = new BufferedReader(new FileReader(theFile));
            String line = null;

            while ((line = br.readLine()) != null) {                
                String[] aLine = line.split("/t");
                br.readLine();
                numLine.add(aLine);
            }
        } catch (IOException e){
        } finally {
            try {
                br.close();
            } catch (Exception e) {
            }
        }

    for (int i=0; i < numLine.size(); i++){
        for (int j = 0; j < numLine.get(i).length; j++){
            System.out.print(numLine.get(i)[j] + " ");
            //  if (!((numLine.get(i)[j]).equals("\t"))){
            intList.add(Integer.parseInt(numLine.get(i)[j]));
            //  }
        }
        System.out.println();
    }
}

And here is what the error says:

Exception in thread "main" java.lang.NumberFormatException: For input string: "6    10  9   10  12  "
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at readingTextFiles.Main.main(Main.java:34)

Please take into account I'm a newbie programmer, got all this from research, so I'm not really sure how the theory works.


回答1:


Change the delimiter as @kayKay mentioned, You are trying to read the line again. I think you shouldn't ??

public static void main(String[] args) {
try {
    br = new BufferedReader(new FileReader(theFile));
    String line = null;

    while ((line = br.readLine()) != null) {
        String[] aLine = line.split("\t"); // Also as kaykay mentioned change /t to \t
        //br.readLine();  // You are reading the line again - Comment it out 
        numLine.add(aLine);
    }
} catch (IOException e){
} finally {
    try {
        br.close();
    } catch (Exception e) {
    }
}

for (int i=0; i < numLine.size(); i++){
    for (int j = 0; j < numLine.get(i).length; j++){
        System.out.print(numLine.get(i)[j] + " ");
        //  if (!((numLine.get(i)[j]).equals("\t"))){
        intList.add(Integer.parseInt(numLine.get(i)[j]));
    }
System.out.println();
}



回答2:


What is the output of the program? The tab character is represented by \t and not /t (line.split("\\t");). Additionally, you will need to add validation checks that each line you read is actually an Integer




回答3:


The error message means you try to parse to an integer something like:

"12abc34d" //<-- bad case - re-consider your approach

or something like:

"   911   " //<-- this can be fixed quickly

, add trim() here:

intList.add(Integer.parseInt(numLine.get(i)[j].trim()));


来源:https://stackoverflow.com/questions/29786653/java-lang-numberformatexception-while-reading-from-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!