0

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)

6
  • Is k supposed to be i ? Commented Mar 7, 2015 at 16:49
  • 1
    What are you seeing? Commented Mar 7, 2015 at 16:49
  • and what is exactly the problem ? Commented Mar 7, 2015 at 16:58
  • Add a line or two that prints out the data you are processing. Print out the strForFirstLine and getFirstLine[i] in each iteration. I suspect the quotes are causing you problems. As you know the first line is a header, and you do not use spaces in the column names, there is no reason to use quotes in the .csv file. Commented Mar 7, 2015 at 17:25
  • The output for getting the firstName: Commented Mar 7, 2015 at 17:29

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.