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();
    }
}
user3058423
  • 69
  • 3
  • 8
  • 1
    Have you tried debugging the code? What does MASTER.csv file contain? – Janar Feb 27 '17 at 21:16
  • 1
    Show the actual content of your `MASTER.csv`, particularly the headers. I think your first column header name is off. – VHS Feb 27 '17 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, – user3058423 Feb 27 '17 at 23:09
  • Master.csv contains aircraft records – user3058423 Feb 27 '17 at 23:09
  • @Janar yes i have tried debugging, see my first comment – user3058423 Feb 28 '17 at 05:03
  • I ran your code and it worked as expected. Post your MASTER.csv file. – Janar Feb 28 '17 at 08:30
  • Its in this zip file http://registry.faa.gov/database/ReleasableAircraft.zip – user3058423 Feb 28 '17 at 15:46

1 Answers1

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.

Community
  • 1
  • 1
Jeronimo Backes
  • 6,141
  • 2
  • 25
  • 29