Converting String in ArrayList

4

I am getting in my java code a variable with the following value

String arquivo = "CNPJ;INSCRICAOESTADUAL;COD_IBGE;DT_OPE;VLR_CARTAO_CRED;VLR_CARTAO_DEB
35083840049;0;4312476;13/01/2018;0.00;66.00
35083840049;0;4312476;18/01/2018;33.00;26.00
35083840049;0;4312476;19/01/2018;0.00;38.40
35083840049;0;4312476;20/01/2018;0.00;55.00
35083840049;0;4312476;21/01/2018;59.80;0.00
35083840049;0;4312476;31/01/2018;0.00;122.00
91589507000854;3770005769;4312476;02/01/2018;2492.59;2742.34
91589507000854;3770005769;4312476;03/01/2018;1333.95;1686.86" 

However, I want to throw these values in an array where, respectively, CNPJ is 35083840049 INSCRICAOESTADUAL is 0 and so on with other values.

When debugging the code I saw that I'm having the following result:

Thatis,itisassemblinganarraywithalltheresult.

I'musingthismethod:

Stringarquivo=arquivoDecodificado;String[]items=arquivo.split(";");
        List<String> itemList = new ArrayList<String>();
        for (String item : items) {
           itemList.add(item);
        }
        System.out.println(itemList);

How can I do to leave the way I need ??

    
asked by anonymous 24.05.2018 / 19:53

1 answer

4

I can suggest that you move this data to a csv file and then use the following code:

Using Scanner:

try {
    Scanner scanner = new Scanner(new File("src/arquivo.csv"));
    // Delimitador dos dados
    scanner.useDelimiter(";");

    while(scanner.hasNext()){
        System.out.print(scanner.next() + " | ");
    }
    scanner.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

The output will be as follows:

CNPJ | INSCRICAOESTADUAL | COD_IBGE | DT_OPE | VLR_CARTAO_CRED | VLR_CARTAO_DEB
35083840049 | 0 | 4312476 | 13/01/2018 | 0.00 | 66.00
35083840049 | 0 | 4312476 | 18/01/2018 | 33.00 | 26.00
35083840049 | 0 | 4312476 | 19/01/2018 | 0.00 | 38.40
35083840049 | 0 | 4312476 | 20/01/2018 | 0.00 | 55.00
35083840049 | 0 | 4312476 | 21/01/2018 | 59.80 | 0.00
35083840049 | 0 | 4312476 | 31/01/2018 | 0.00 | 122.00
91589507000854 | 3770005769 | 4312476 | 02/01/2018 | 2492.59 | 2742.34
91589507000854 | 3770005769 | 4312476 | 03/01/2018 | 1333.95 | 1686.86 |

Using BufferedReader:

try(BufferedReader buffer = new BufferedReader(new FileReader("src/arquivo.csv"))) {

    String line;

    while ((line = buffer.readLine()) != null) {
        // String [] dados = line.split(";"); // Necessário para separar os dados
        System.out.println(line);
    }

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Output:

35083840049;0;4312476;13/01/2018;0.00;66.00
35083840049;0;4312476;18/01/2018;33.00;26.00
35083840049;0;4312476;19/01/2018;0.00;38.40
35083840049;0;4312476;20/01/2018;0.00;55.00
35083840049;0;4312476;21/01/2018;59.80;0.00
35083840049;0;4312476;31/01/2018;0.00;122.00
91589507000854;3770005769;4312476;02/01/2018;2492.59;2742.34
91589507000854;3770005769;4312476;03/01/2018;1333.95;1686.86

Dai it is enough that you build your object within the while.

Note: Skip a line if the data has a header.

Sources:

link link

    
24.05.2018 / 20:50