【问题标题】:Java read text and manipulate text data as integerJava读取文本并将文本数据操作为整数
【发布时间】:2014-09-03 08:15:49
【问题描述】:

我正在尝试读取这样的 csv 文件:

0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0,
0, 1, 2, 1, 0, 0, 1, 0,
1, 1, 0, 0, 10, 0,
0, 0, 0, 0, 0, 1, 1, 2, 1, 0,
0, 0, 0, 1, 0, 0, 0,

并转换为整数格式并对每一行求和,但不知道为什么会出错,我的预期结果应该是这样的:

[exam 1][LWD 5]
[exam 2][LWD 5]
[exam 3][LWD 12]
[exam 4][LWD 5]
[exam 5][LWD 1]

这是我的 java 编码,我使用的是 CSVReader 库:

public static void main(String[] args) throws IOException {
    //read text file
    CSVReader a = new CSVReader(new FileReader("test.txt"));
    //store text data into arraylist
    List<String[]> aa = a.readAll();
    //create 2D array LWD
    int LWD[][] = new int[aa.size()][2];
    //store data to array LWD
    for (int i = 0; i < aa.size(); i++) {
        for (int x = 0; x < aa.get(i).length; x++) {
            LWD[i][1] += Integer.parseInt(aa.get(i)[x].trim());
        }
        LWD[i][0] = i+1;
    }
    //display LWD
    for (int i = 0; i < aa.size(); i++) {
    System.out.print("[exam " + LWD[i][0] + "]");
    System.out.print("[LWD " + LWD[i][1] + "]");
    System.out.print("\n");
    }
}

这是我得到的错误结果:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at test.Test.main(Test.java:20)
Java Result: 1

【问题讨论】:

  • 在 csv 格式中,您没有空格。你得到了什么错误/结果?
  • 确认一下,哪一行代码是第20行?
  • 这是第 20 行:LWD[i][1] += Integer.parseInt(aa.get(i)[x].trim());
  • +1 它只是帮助我练习更多的 lambda 表达式

标签: java string csv integer format


【解决方案1】:

问题似乎是 CSVReader 由于尾随逗号而在每行末尾给出了一个空字符串。

例如,当我运行您的确切代码,但从 csv 文件中删除尾随逗号时,如下所示:

0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0
0, 1, 2, 1, 0, 0, 1, 0
1, 1, 0, 0, 10, 0
0, 0, 0, 0, 0, 1, 1, 2, 1, 0
0, 0, 0, 1, 0, 0, 0

它给出了正确的输出。

我想您可以通过跳过每个字符串数组中的最后一个字符串或更改 csv 文件来解决此问题

编辑:
这是一个适用于当前 csv 格式的快速修复:

public static void main(String[] args) throws IOException {
    //read text file
    CSVReader a = new CSVReader(new FileReader("test.txt"));
    //store text data into arraylist
    List<String[]> aa = a.readAll();
    //create 2D array LWD
    int LWD[][] = new int[aa.size()][2];
    //store data to array LWD
    for (int i = 0; i < aa.size(); i++) {
        for (int x = 0; x < aa.get(i).length-1; x++) {
            LWD[i][1] += Integer.parseInt(aa.get(i)[x].trim());
        }
        LWD[i][0] = i+1;
    }
    //display LWD
    for (int i = 0; i < aa.size(); i++) {
        System.out.print("[exam " + LWD[i][0] + "]");
        System.out.print("[LWD " + LWD[i][1] + "]");
        System.out.print("\n");
    }
}

我只更改了第 10 行,用于(int x = 0; x &lt; aa.get(i).length-1; x++)x &lt; aa.get(i).length 更改为x &lt; aa.get(i).length - 1 检查字符串是否为空可能会更优雅,您可能希望根据 csv 文件的其余部分来执行此操作。

【讨论】:

    【解决方案2】:

    每行以“,”结尾。 CSVReader 认为末尾有一个空字符。 当你调用它时

    Integer.parseInt(aa.get(i)[x].trim())
    

    aa.get(i)[x].trim() 在到达行尾时为每一行返回 ""。

    【讨论】:

      【解决方案3】:

      我知道你想弄清楚你的问题,而且你已经有了答案。

      我只是以这种方式发布,以便为您提供更多种类,减少头痛。我用 lambda 表达式解决了

      代码:

      String[][] numbers = {{"0", "0", "0", "1", "0", "1", "0", "1", "0", "1", "1", "0"},
          {"0", "1", "2", "1", "0", "0", "1", "0"},
          {"1", "1", "0", "0", "10", "0"},
          {"0", "0", "0", "0", "0", "1", "1", "2", "1", "0"},
          {"0", "0", "0", "1", "0", "0", "0"}};
      
          List<String> l = new ArrayList<>();
          for (int i = 0; i < numbers.length; i++) {
              for (int j = 0; j < numbers[i].length; j++) {
                  l.add(numbers[i][j]);
      
              }
              System.out.print("[exam " + (i + 1) + "]");
              System.out.print("[LWD " + l.stream().map(s -> Integer.parseInt(s)).reduce((a, b) -> a + b).get() + "]\n");
              l.clear();
      
          }
      

      输出:

      [exam 1][LWD 5]
      [exam 2][LWD 5]
      [exam 3][LWD 12]
      [exam 4][LWD 5]
      [exam 5][LWD 1]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多