Java读取文本并将文本数据操作为整数

时间:2014-07-13 03:01:42

标签: java string csv integer format

我试图读取这样的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

3 个答案:

答案 0 :(得分:3)

问题似乎是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

它提供了正确的输出。

我想你可以通过跳过每个String数组中的最后一个String或更改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 < aa.get(i).length-1; x++) 已将x < aa.get(i).length更改为x < aa.get(i).length - 1 检查字符串是否为空可能更为优雅,您可能希望这取决于csv文件的其余部分。

答案 1 :(得分:2)

每行以&#34;,&#34;结尾。 CSVReader认为最后有一个空字符。 当你打电话给这个

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

aa.get(i)[x].trim()返回&#34;&#34;当每一行到达行的末尾时。

答案 2 :(得分:1)

我知道您想解决问题,而且您已经得到了答案。

我只是以这种方式发布,为您提供更多种类,减少头痛。我解决了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]