1

I am using JavaCSV to read and write CSV files, one issue i have found is that it ignores data in the first column in part of my program

String nNumber = planes.get("N-NUMBER"); returns nothing, even though there is something in the database

CsvReader http://javacsv.sourceforge.net/com/csvreader/CsvReader.html

import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.String;
import java.io.File;
import java.io.FileWriter;
import java.lang.String;
import com.csvreader.CsvReader;

private static void compare(String firstName, String lastName, String st1, String st2, String city, String state, String zip,
        String country, String region, String fileName, Integer pilotCount) 
{
    // TODO Auto-generated method stub
    try {

        CsvReader planes = new CsvReader("MASTER.csv");
        planes.readHeaders();

        while (planes.readRecord())
        {
            String nNumber = planes.get("N-NUMBER");
            String SN = planes.get("SERIAL NUMBER");
            String model = planes.get("MFR MDL CODE");
            String engine = planes.get("ENG MFR MDL");
            String year = planes.get("YEAR MFR");
            String typeReg = planes.get("TYPE REGISTRANT");
            String name = planes.get("NAME");
            String st_1 = planes.get("STREET");
            String st_2 = planes.get("STREET2");
            String city_plane = planes.get("CITY");
            String pilot_state = planes.get("STATE");
            String zip_code = planes.get("ZIP CODE");
            String country_name = planes.get("COUNTRY");
            String region_name = planes.get("REGION");
            //boolean response = compare(st1, st2, city, state, zip, country, region, st_1, st_2, city_plane, pilot_state, zip_code, country_name, region_name);
            boolean response = compareSimple(firstName, lastName, name);

            if (response == true)
            {
                System.out.println("N Number");
                System.out.println(nNumber);
                System.out.println("Serial");
                System.out.println(SN);
                System.out.println("Model");
                System.out.println(model);
                System.out.println("Engine");
                System.out.println(engine);
                System.out.println("Year");
                System.out.println(year);
                System.out.println("Type Registration");
                System.out.println(typeReg);
                System.out.println("Name");
                System.out.println(name);
                pilotCount++;
                write(nNumber, SN, model, engine, year, typeReg, name, st_1, st_2, city_plane, pilot_state, zip_code, country_name, region_name, fileName);
                System.out.println(pilotCount);
            }
            else if (response == false)
            {
                // do nothing
            }
        }
        planes.close();
    }
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
}
7
  • 1
    Have you tried debugging the code? What does MASTER.csv file contain? Commented Feb 27, 2017 at 21:16
  • 1
    Show the actual content of your MASTER.csv, particularly the headers. I think your first column header name is off. Commented Feb 27, 2017 at 21:16
  • N-NUMBER,SERIAL NUMBER,MFR MDL CODE,ENG MFR MDL,YEAR MFR,TYPE REGISTRANT,NAME,STREET,STREET2,CITY,STATE,ZIP CODE,REGION,COUNTY,COUNTRY,LAST ACTION DATE,CERT ISSUE DATE,CERTIFICATION,TYPE AIRCRAFT,TYPE ENGINE,STATUS CODE,MODE S CODE,FRACT OWNER,AIR WORTH DATE,OTHER NAMES(1),OTHER NAMES(2),OTHER NAMES(3),OTHER NAMES(4),OTHER NAMES(5),EXPIRATION DATE,UNIQUE ID,KIT MFR, KIT MODEL,MODE S CODE HEX, Commented Feb 27, 2017 at 23:09
  • Master.csv contains aircraft records Commented Feb 27, 2017 at 23:09
  • @Janar yes i have tried debugging, see my first comment Commented Feb 28, 2017 at 5:03

1 Answer 1

1

Possibly related to this one?

You are probably reading this with an incorrect encoding, or your file has a BOM marker, in this case the file starts with a few bytes indicating what is the encoding. In that case you can just skip these bytes to get the correct output:

InputStream in = new FileInputStream("/path/to/file.csv", "UTF-8");

//this skips UTF-8 BOM bytes, adjust to discard the bytes of whatever you have:

if(in.read() == 239 & in.read() == 187 & in.read() == 191){
    System.out.println("UTF-8 with BOM, bytes discarded");
}

//parse the InputStream now as the BOM bytes have been skipped.

If you are sure this is not the case and there is not a BOM marker, then try switching to another library to see if the problem persists.

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

1 Comment

changing the encoding from UTF-8 BOM to UTF-8 fixed the problem.

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.