Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java read text and manipulate text data as integer

i'm trying to read a csv file like this:

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,

and convert into integer format and sum up for each row, but dunno why it's got error, my expected outcome should be like this:

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

this is my java coding and i was using CSVReader library:

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");
    }
}

this is the error result i get:

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
like image 848
user3832964 Avatar asked Aug 13 '26 02:08

user3832964


1 Answers

The issue appears to be that CSVReader is giving an empty string at the end of each line because of the trailing commas.

For example, when I run your exact code, but remove the trailing commas from the csv file like this:

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

It gives the proper output.

I suppose you could either account for this by skipping the last String in each of the String arrays or changing the csv file(s)

Edit:
Here's a quick fix that should work with the current csv format:

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");
    }
}

All I changed was line 10, for (int x = 0; x < aa.get(i).length-1; x++) Changed x < aa.get(i).length to x < aa.get(i).length - 1 It might be more elegant to check if the string is empty instead, you might want to do that depending on the rest of the csv files.

like image 158
Nick Meyer Avatar answered Aug 14 '26 17:08

Nick Meyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!