Converting String to Arraylist

Asked

Viewed 888 times

4

I am receiving 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 play these values in an array where, respectively, CNPJis 35083840049 INSCRICAOESTADUAL is 0 and so successively with the other values.

When debugging the code I saw that I am having the following result:

inserir a descrição da imagem aqui

i.e., it is mounting an array with all the result.

I’m using this method:

     String arquivo = 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 make it the way I need it ??

  • It would not be easier to read line by line a file . csv and make an object Arraylist with the fields?

  • @Gustavofragoso has some example to show ?

1 answer

4


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

Using a 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 exit 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();
}

Exit:

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

It is enough that you build your object inside the while.

Note: Skip a row if the data is header.

Sources:

http://www.java67.com/2015/08/how-to-load-data-from-csv-file-in-java.html https://stackoverflow.com/questions/14274259/read-csv-with-scanner

  • Out of curiosity, there is some way to do this in java7/8 without using a scanner?

  • I did a search and edited with another format @Articuno. I took the opportunity to put references.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.