【问题标题】:NumberFormatException when I try to read text file of integers into a int[]当我尝试将整数文本文件读入 int[] 时出现 NumberFormatException
【发布时间】:2020-09-22 04:18:10
【问题描述】:

我很困惑。我试图将文本文件的 10 行中的 10 个整数放入 int[] 中。我尝试了几种不同的方法,但我最近的尝试是对文件的每一行在 BufferedReader.readLine() 上使用 for 循环和 parseInt。这每次都返回 NumberFormatException。


public class InversionCounter {

    static int[] fileToArray(String filename) {
        File file = new File(filename);
        try ( BufferedReader br = new BufferedReader(new FileReader(file))) {
        int numOfLine = Integer.parseInt(br.readLine());
        int[] ourArray = new int[10];

        for (int i = 0; i < ourArray.length; i++)  {
            ourArray[i] = numOfLine;
            numOfLine = Integer.parseInt(br.readLine());
        }


        return ourArray;

        }  catch (NumberFormatException e) {
        System.out.println("NumberFormatException");
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        } catch (IOException e) {
            System.out.println("IO Exception");
        }

        return null;
    }

    public static void main(String[] args) throws IOException {
        fileToArray("/home/paris/coolfile");

    }



        }

错误:

Exception in thread "main" java.lang.NumberFormatException: null
    at java.base/java.lang.Integer.parseInt(Integer.java:614)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at InversionCounter.fileToArray(InversionCounter.java:16)
    at InversionCounter.main(InversionCounter.java:30)

文件就是这样的:

45
67
87
76
7
4
5
23
5675
3

【问题讨论】:

  • 对不起,返回的 null 应该在 catch 块内。

标签: java io bufferedreader


【解决方案1】:

当文件的行在这里被读取时出现问题:

int numOfLine = Integer.parseInt(br.readLine());

引用BufferedReader.readLine()的javadoc

返回:一个包含行内容的字符串,不包括任何行终止字符,如果已经到达流的末尾,则返回 null 没有读取任何字符。

你需要在解析前测试读取的行是否为空,否则Integer.parseInt()会抛出NumberFormatException

这是一个简单的修复:

String line = br.readLine();
if (line == null) {
    break;
}
int numOfLine = Integer.parseInt(line);

更好的方法是不假设一定数量的输入,只读取行直到文件末尾(即直到行为空),将它们添加到List&lt;Integer&gt;

【讨论】:

    【解决方案2】:

    文件中只有 10 行,但您的 readLine 指针正试图读取第 11 行。发生这种情况是因为您在循环外读取了一行,而现在尝试在循环中读取 10 行。以下代码应该可以工作:

    public class InversionCounter {
        static int[] fileToArray(String filename) {
            File file = new File(filename);
            try ( BufferedReader br = new BufferedReader(new FileReader(file))) {
            int numOfLine = Integer.parseInt(br.readLine());
            int[] ourArray = new int[10];
    
            for (int i = 0; i < (ourArray.length - 1); i++)  {
                ourArray[i] = numOfLine;
                numOfLine = Integer.parseInt(br.readLine());
            }
    
    
            return ourArray;
    
            }  catch (NumberFormatException e) {
            System.out.println("NumberFormatException");
            } catch (FileNotFoundException e) {
                System.out.println("File not found");
            } catch (IOException e) {
                System.out.println("IO Exception");
            }
    
            return null;
        }
    
        public static void main(String[] args) throws IOException {
            fileToArray("/home/paris/coolfile");
    
        }
    
    
    }
    

    P.S:另外,如果有多余的空格,你应该修剪线条。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 2012-10-30
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多