简体   繁体   中英

Trouble with manipulating data from a CSV file(JAVA)

it might sound stupid. I am trying to read and process the data from a CSV file. I save the first line into a String array(things such Year, Month, Day, First name, Second name, date of birth, nationality). Well For example I need just Year, date of birth, and the First name. As I have many CSV files, and the order of the header(the first line) is changing I have to link some variables with the position of the Year, date of birth and first name from the array. So I tryed a lot of posibilities. One of them is here:

    int indexYear = 0;
    int indexMonth = 0;
    int indexDay = 0;
    int indexFirstname = 0;
    int indexSecondname = 0;
    String strForFirstLine="";

    strForFirstLine += input.readLine();
    String getFirstLine[] = strForFirstLine.split(",");
    for(int i=0; i<getFirstLine.length; ++i){
        if(getFirstLine[i].equals("'Year'"))
            indexYear = i;
        if(getFirstLine[i].equals("'Month'"))
            indexMonth = i;
        if(getFirstLine[i].equals("'Day'"))
            indexDay = i;
        if(getFirstLine[i].equals("'Firstname'"))
            indexFirstName = i;
        if(getFirstLine[i].equals("'Secondname'"))
            indexSecondName = i;
    }

Thanks in advance :).

output for getting the SecondName from an arrayList:

The output for getting the firstName: 'code' run:

"2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" "2012/3" null BUILD SUCCESSFUL (total time: 0 seconds)

Try CSVReader Api , which will make your life much easier

Example

CsvReader products = new CsvReader("products.csv");

            products.readHeaders();

            while (products.readRecord())
            {
                String productID = products.get("ProductID");
                String productName = products.get("ProductName");
                String supplierID = products.get("SupplierID");
                String categoryID = products.get("CategoryID");
                String quantityPerUnit = products.get("QuantityPerUnit");
                String unitPrice = products.get("UnitPrice");
                String unitsInStock = products.get("UnitsInStock");
                String unitsOnOrder = products.get("UnitsOnOrder");
                String reorderLevel = products.get("ReorderLevel");
                String discontinued = products.get("Discontinued");

                // perform program logic here
                System.out.println(productID + ":" + productName);
            }

            products.close();

Then Add whatever you want to add in your ArrayList

Source : CSV Reader In Java

Use uniVocity-parsers CsvParser:

CsvParserSettings settings = new CsvParserSettings();
//defines the order of the fields you are interested in reading.
parserSettings.selectFields("ProductID", "ProductName", etc...);

CsvParser parser = new CsvParser(settings);
//returns the rows at the specified order
List<String[]> rows = parser.parseAll(new FileReader("your_input_file"));

It doesn't matter how the headers of each CSV input you get are ordered. The parser will grab the correct values from the input. If the CSV does not have a header you selected, null will be returned.

Check the documentation, there are many options to help you processing all sorts of wild inputs.

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

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