Search code examples
javacsvnumberstrim

Use Java import CSV file number into array, strange "whitespace" cannot be trimmed


I am using java to import doubles from a excel file into doubles array, here is my code(I steal the second half of the code from another source: https://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/):

     private static int rows = 10;
     private static double[][][] data10;

     data10 = new double[rows][2][];
     for (int i = 0; i<rows; i++) {
        data10[i][0] = new double[16];
        data10[i][1] = new double[1];
     }
    String csvFile = "data.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    try {

        br = new BufferedReader(new FileReader(csvFile));

        for (int j = 0; j < rows; j++) {
            line = br.readLine();
            // use comma as separator
            String[] values = line.split(cvsSplitBy);

            for (int i = 0; i < values.length-1; i++) {
                //String temp = values[i];
                //System.out.println(temp.trim());
                //System.out.println(2);
                data10[j][0][i] = Double.valueOf(values[i]);
            }
            data10[j][1][0] = Double.valueOf(values[values.length - 1].trim());
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

i had numberformatexception at this line:

data10[j][0][i] = Double.valueOf(values[i]);

the reason is that the the first cell in the csv file is number 2, however it was shown as " 2" in the exception message. I tried to trim the string, however the "whitespace" cannot be trimmed. I tried to assign values[i] to a String object and trim it yet without success.

Finally I realized that I have to save the file as "comma separated values(.csv)", but not "CSV UTF-8(comma delimited)(.csv)". After correcting this, the code works.

I hope this would help someone. I can't find the answer in other stackoverflow posts.


Solution

  • .trim() replaces whitespace which is represented by unicode '\u0020'. What you might be seeing is a nobreakspace(&nbsp) which is represented by '\u00A0'.

    To remove it try: data10[j][0][i] = Double.valueOf(values[i].replaceAll("\\u00A0", ""));