简体   繁体   中英

How to modify a given String (from CSV)

I need to write a program for a project at university which should cut some specific parts out of a given CSV File. I've started already but I don't know how to keep only the content (sentence and vote values) or min. to remove the date part.

PARENT,"Lorem ipsum...","3","0","Town","09:17, 29/11/2016"

REPLY,"Loren ipsum...”,"2","0","Town","09:18, 29/11/2016"

After the program ran I want to have it like this:

Lorem ipsum... (String) 3 (int) 0 (int)

Loren ipsum... (String) 2 (int) 0 (int)

I have no problem with writing a parser (read in, remove separators) but I don't know how realize this thing.

You can create your own data structure that contains a string, and two integers and then do the following while reading from the csv file. Only include the stuff you want from the csv based on the column number which is the index of the String array returned by the split() method.

     Scanner reader = new Scanner(new File("path to your CSV File"));
     ArrayList<DataStructure> csvData = new ArrayList<>();

     while(reader.hasNextLine())
     {
         String[] csvLine = reader.nextLine().split(",");
         DataStructure data = new DataStructure(
                                      csvLine[1],
                                      Integer.parseInt(csvLine[2]), 
                                      Integer.parseInt(csvLine[3]));
         csvData.add(data);
     }

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