How to read data from a .csv file and save only some in an arraylist

1

I am reading a .csv file as follows

BufferedReader rd = new BufferedReader( new FileReader ("C:/Users/user/Documents/running-example.csv"));
         String line = null;
          ArrayList lista = new ArrayList();

        while ((line = rd.readLine()) != null) {

            line = rd.readLine();
            System.out.println(line);
            lista.add(line);

             }

and I would like to know how to search the registered activities within the registers, in this case as: register request, check ticket ... etc. And save them in an arrayList or Array. But which way or method can I use to select or save the registered activity?

1;35654423;30-12-2010:11.02;register request;Pete;50
1;35654425;05-01-2011:15.12;check ticket;Mike;100
1;35654427;07-01-2011:14.24;reject request;Pete;200
2;35654485;30-12-2010:12.12;check ticket;Mike;100
2;35654488;05-01-2011:11.22;decide;Sara;200
3;35654521;30-12-2010:14.32;register request;Pete;50
3;35654524;30-12-2010:16.34;check ticket;Ellen;100
3;35654526;06-01-2011:12.18;reinitiate request;Sara;200
3;35654530;08-01-2011:11.43;check ticket;Pete;100
3;35654533;15-01-2011:10.45;pay compensation;Ellen;200
4;35654643;07-01-2011:12.06;check ticket;Mike;100
4;35654645;09-01-2011:12.02;decide;Sara;200
5;35654711;06-01-2011:09.02;register request;Ellen;50
5;35654714;08-01-2011:11.22;check ticket;Pete;100
5;35654716;11-01-2011:16.18;reinitiate request;Sara;200
5;35654719;16-01-2011:15.50;examine casually;Mike;400
5;35654721;20-01-2011:12.48;reinitiate request;Sara;200
5;35654724;21-01-2011:11.34;check ticket;Pete;100
5;35654726;24-01-2011:14.56;reject request;Mike;200
6;35654873;06-01-2011:16.06;examine casually;Ellen;400
6;35654875;07-01-2011:16.52;decide;Sara;200
    
asked by Rita diaz 26.02.2018 в 19:39
source

1 answer

1

The split method, for strings, starts a string based on a string (or a regular expression) and returns an array of the string divided according to the separator.

In your case, when reading each line, what you have to do is something like this:

//definir el vector que contendra la linea separada
String[] linea;
.....
line = rd.readLine();
linea = line.Split(";")
....
//Guardar en el array solo la posicion que queres:
lista.add(linea[3]);

That way the list will only be the part you need.

The complete and corrected code would be:

BufferedReader rd = new BufferedReader( new FileReader ("C:/Users/user/Documents/running-example.csv"));
String line = null;
ArrayList lista = new ArrayList();
String[] linea;
while ((line = rd.readLine()) != null) {
    line = rd.readLine();
    System.out.println(line);
    linea = line.Split(";")
    lista.add(linea[3]);
}

Do not correct the code itself or delete variables that may no longer work, just so that the example is understandable based on your current code.

Split on the web

    
answered by 26.02.2018 / 20:24
source