【问题标题】:Need help fixing NumberFormatException error [duplicate]需要帮助修复 NumberFormatException 错误 [重复]
【发布时间】:2021-03-18 23:38:17
【问题描述】:

我正在尝试读取一个类似于此的文本文件:

0000000000 
0000100000
0001001000
0000100000
0000000000

这是我的代码:

public static int[][] readBoard(String fileName) throws FileNotFoundException {
    File life = new File(fileName);
    Scanner s = new Scanner(life);

    int row = s.nextInt();
    int columns = s.nextInt();
    int [][] size = new int [row][columns];
    
    for (int i=0; i <= row; i++) {
        String [] state = new String [columns];
        String line = s.nextLine();
        state = line.split("");
        for (int j=0; i <= columns; i++) {
            size[i][j] = Integer.parseInt(state[j]); 
        }
    }
    
    return size;
}

它一直给我这个错误。我认为是Integer.parseInt(state[j]) 给我带来了麻烦,但我不知道为什么。

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:662)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at Project5.readBoard(Project5.java:33)
    at Project5.main(Project5.java:9)

【问题讨论】:

  • 它说你的字符串是空的。您是否在代码中添加了任何 System.out.println(...) 语句以查看:1)“line”变量的值是什么,2)“state[j]”的值是什么?这是基本调试。
  • 您以错误的方式使用 Scanner 对象来获取列数和行数。您必须逐行读取文件以获得必要的行数和列数。因此使用 ArrayList 而不是 Array,因为使用 ArrayList 您可以动态追加项目。

标签: java parsing integer


【解决方案1】:

我已经使用示例输入执行了您的代码,但您在代码中存在逻辑问题。使用示例输入,代码甚至不会到达parseInt() 行,在该行中可能会抛出询问的NumberFormatException。我假设您已经在不同的输入中尝试过您的代码。异常消息是 staithforward,您尝试将空字符串解析为数字。这是一个典型的NumberFormatExceptionparseInt() 函数会抛出异常,所以你的代码必须为此做好准备。

另一个问题是算法中的基本逻辑问题。您的 rowcolumn 变量将使用文本中的第一个到整数标记填充。根据示例输入,第一个整数标记将是第一行0000000000 ,其整数值为0,第二个标记是0000100000,将解析为100000。所以你试图用这些不可能的维度初始化一个数组。

要计算行数,您必须逐行读取文件。要获得列数,您可以检查行的长度。 (它可以打开一个新问题,你想如何处理格式不正确的输入文件,因为文件中的行长度可以是不同的。)

这意味着只有在您已经迭代过文件内容的情况下,您才能确定电路板的尺寸。为了防止多次迭代,您应该使用动态集合而不是标准数组,例如 ArrayList。

这意味着当您逐行读取文件时,您可以在一行中一个接一个地处理字符。在此步骤中,您应该考虑文件末尾的无效字符和潜在的空字符。在这个迭代过程中,最终的集合可以被构建。

这个例子展示了一个有效的解决方案:

private static int processCharacter(char c) {
    try {
        return Integer.parseInt((Character.toString(c)));
    } catch (NumberFormatException e) {
        return 0;
    }
}

public static List<List<Integer>> readBoard(String fileName) throws FileNotFoundException {
    List<List<Integer>> board = new ArrayList<>();
    File file = new File(fileName);
    FileReader fr = new FileReader(file);

    try (BufferedReader br = new BufferedReader(fr)) {
        String line;
        while ((line = br.readLine()) != null) {
            line = line.trim(); // removes empty character from the line
            List<Integer> lineList = new ArrayList<>();
            if(line.length() > 0) {
                for (int i = 0; i < line.length(); i++) {
                    lineList.add(Main.processCharacter(line.charAt(i)));
                }
                board.add(lineList);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return board;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    相关资源
    最近更新 更多