I need to parse a one-column CSV file that not only has extra commas but also some of the names include extra quotes. I have looked over and have read the other previous questions and one of the best answers was Achintya Jha's Answer. However, that solution does not seem to work in my case. An example is that the name
ADAMS COUNTY SHERIFF "ADAMS COUNTY SHERIFF'S OFFICE, CO"
is being printed out as:
ADAMS COUNTY SHERIFF
"ADAMS COUNTY SHERIFF'S OFFICE, CO"
It is splitting at the correct spots and is taking care of the extra commas but not it is hitting the extra quotes and is splitting there now too, so String csvSplitBy = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
will not work. Does anyone know of another way to handle this issue in Java? Other have asked this question for an answer in other languages but I could not find any, other then the one i linked to, about Java. Thanks!
This is my Java code:
package csvdatacompareapplication;
import java.io.*;
public class CSVDataCompareApplication {
public static void main(String[] args) {
BufferedReader br = null;
BufferedReader br2 = null;
String customerListAllCustomers = "C:\\Users\\Desktop\\customerListAllCustomers.csv";
String customerListToRemove = "C:\\Users\\Desktop\\customerListToRemove.csv";
String line = "";
String csvSplitBy = ",";
try {
br = new BufferedReader(new FileReader(customerListAllCustomers));
while ((line = br.readLine()) != null) {
// use comma as separator
//String [] customersAll = line.split(csvSplitBy);
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
First few lines of my .CSV File
ADAMS COUNTY SHERIFF'S OFFICE, CO
ADAMSON POLICE PRODUCTS
ADAN DAVILA
ADAPT SECURE
ADDISON PD - MIKE VINCENT
ADDISON POLICE - IL
ADDISON PORTER
ADIN MCGARVIE
ADMIRAL FIRE & SAFETY
ADMON IRAMIYA
ADRIAN DANG
ADRIAN HUMPHRIES
ADRIAN KEPKA
ADRIAN SALDANA
ADRIAN SOLER
ADRIAN YORK
ADRIENNE BAKER
ADRIENNE MOOS
ADS INC.
ADS, INC
I updated my java code and now this is what prints out
"ADAMS COUNTY SHERIFF'S OFFICE, CO"
ADAMSON POLICE PRODUCTS
ADAN DAVILA
ADAPT SECURE
ADDISON PD - MIKE VINCENT
ADDISON POLICE - IL
ADDISON PORTER
ADIN MCGARVIE
ADMIRAL FIRE & SAFETY
ADMON IRAMIYA
ADRIAN DANG
ADRIAN HUMPHRIES
ADRIAN KEPKA
ADRIAN SALDANA
ADRIAN SOLER
ADRIAN YORK
ADRIENNE BAKER
ADRIENNE MOOS
ADS INC.
"ADS, INC"
Why did the quotes get placed in?