简体   繁体   中英

Java: Import CSV problem with leading zero

I'm trying to import csv file, but leading zeros makes me problem.

Structure of CSV:

code1 | code2 | code3
-----------------------
00101 | 00021 | 0589654
00101 | 00022 | 0589654
00101 | 00023 | 0589654
00101 | 00024 | 0589654

Here is my code for reading CSV:

private static List<Code> readCodesFromCSV(String fileName) {
        List<Code> codes = new ArrayList<>();
        Path pathToFile = Paths.get(fileName);

        // create an instance of BufferedReader
        // using try with resource, Java 7 feature to close resources
        try (BufferedReader br = Files.newBufferedReader(pathToFile)) {

            // read the first line from the text file
            String line = br.readLine();

            // loop until all lines are read
            while (line != null) {

                // use string.split to load a string array with the values from
                // each line of
                // the file, using a comma as the delimiter
                String[] attributes = line.split(",");

                Code code = createCode(attributes);

                // adding book into ArrayList
                codes.add(code);

                // read next line before looping
                // if end of file reached, line would be null
                line = br.readLine();
            }

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return codes;
    }

    private static Code createCode(String[] metadata) {
        String code = "\"\t" + metadata[0] + "\"" + "\"\t" + metadata[1] + "\"" + "\"\t" + metadata[2] + "\"";
        //String code = metadata[0] + metadata[1] + metadata[2];;


        // create and return code of this metadata
        return new Code(code);
    }

I need to join all 3 codes into one, but the problem are leading zeros. When i import file and get merged code i get code without leading zeros, so i try with adding \\t but it doesn't help. Does anyone know how can i solve this problem?

Would not be simpler to write a new constructor, that takes a String[] argument, instead of gluing the Strings together in some custom format to be parsed by the current constructor?

BTW your constructor would be called with a String like this:
(I left \\t intact, all other characters are in the String value...)

"\t00101""\t00021""\t0589654"

Why dont you just replace your Code datatype with String and once you have the correct codes List from each line, parse or convert it to whatever you want from there.

private static List<String> readCodesFromCSV(String fileName) throws IOException {
        List<String> codes = new ArrayList<>();
        Path pathToFile = Paths.get(fileName);

        // create an instance of BufferedReader
        // using try with resource, Java 7 feature to close resources
        try (BufferedReader br = Files.newBufferedReader(pathToFile)) {

            // read the first line from the text file
            String line = br.readLine();

            // loop until all lines are read
            while (line != null) {

                // use string.split to load a string array with the values from
                // each line of
                // the file, using a comma as the delimiter
                String[] attributes = line.split(",");

                String code = createCode(attributes);

                // adding book into ArrayList
                codes.add(code);

                // read next line before looping
                // if end of file reached, line would be null
                line = br.readLine();
            }

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return codes;
    }

    private static String createCode(String[] metadata) {
        String code = metadata[0] + metadata[1] + metadata[2];
        return code;
    }

As mentioned, its not clear from the code, what is happening in the Code constructor, but assuming there is the issue,

in parsing values with leading zeroes to some numbered datatype, check bellow example of parsing String (with leading zero) into int datatype:

Code is taking String array with values with leading zeroes and parsing into String with separator , second method parse the same array into int by Integer.parseInt()

public static void testMe() {
    String[] testData = {"00101","00021","0589654","00101","00022"};
    System.out.println("INPUT: ");
    for (int i = 0; i < testData.length; i++) {
        System.out.print(testData[i] + ",");
    }

    System.out.println("\n");
    String stringData = loadStringArrayToString(testData);
    System.out.println("check, its parsed with leading zeros: \n" + stringData +"\n");

    int[] intData = loadStringArrayToIntArray(testData);
    System.out.println("check, zeros are gone (because integer as datatype):");
    for (int i = 0; i < intData.length; i++) {
        System.out.print(intData[i] + ", ");
    }

}


public static String loadStringArrayToString(String[] inputData) {
    StringBuilder sb = new StringBuilder();
    char separator = ';';

    // -1 to avoid last separator
    for (int i = 0; i < inputData.length -1; i++) {
        sb.append(inputData[i]);
        sb.append(separator);
    }
    sb.append(inputData[inputData.length-1]);

    return sb.toString();
}

public static int[] loadStringArrayToIntArray(String[] inData) {
    int[] retData = new int[inData.length];

    for (int i = 0; i < inData.length; i++) {
        retData[i] = Integer.parseInt(inData[i]);
    }

    return retData;
}

Output is:

INPUT: 
00101,00021,0589654,00101,00022,

check, its parsed with leading zeros: 
00101;00021;0589654;00101;00022

check, zeros are gone (because integer as datatype):
101, 21, 589654, 101, 22, 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM