I'm trying to parse some data sets to through into a database. The CSV files that i'm working with have the data separated by commas. However there is some data that is encompassed by quotation marks which include commas that are part of that string. How do i step through each line of data and parse out each data entry given this obstacle?
edit: CSV parsers are not an option because this program will be used for non csv files. And i'm trying to minimize external libraries.
I found the issue because of my code here would just parse through the quotes:
while(bufferinput.hasNext()){
nextline = bufferinput.nextLine();
dataArray = nextline.split(",");
for(i = 0; i<colNum;i++){
//Try-Catch to search through dataArray and push it into sqlData arraylist.
try{
if(!dataArray[i].toString().isEmpty()){
sqlData.add(dataArray[i].toString());
}
//else if used to find empty cells between non empty cells (Have to test to see if opposite of if-statement works)
else if(dataArray[i].toString().equals("")){
sqlData.add(null);
}
}
//Catch is necessary to find the cells not at the end of the row in a text file.
catch(ArrayIndexOutOfBoundsException e){
sqlData.add(null);
}
}
}
An example of this data would be something like ,,"Ontario, Canada",xxxxx.....
Locations are the biggest cause of it as the address contains a comma.