String[] values = line.split(",");
Long locId = Long.parseLong(replaceQuotes(values[0]));
String country = replaceQuotes(values[1]);
String region = replaceQuotes(values[2]);
String city = replaceQuotes(values[3]);
String postalCode = replaceQuotes(values[4]);
String latitude = replaceQuotes(values[5]);
String longitude = replaceQuotes(values[6]);
String metroCode = replaceQuotes(values[7]);
String areaCode = replaceQuotes(values[8]);
//...
public String replaceQuotes(String txt){
txt = txt.replaceAll("\"", "");
return txt;
}
I'm using the code above to parse a CSV with data in this format:
828,"US","IL","Melrose Park","60160",41.9050,-87.8641,602,708
However, when I encounter a line of data such as the following I get java.lang.ArrayIndexOutOfBoundsException: 7
1,"O1","","","",0.0000,0.0000,,
Does this mean that any time I even try to access the value at values[7], an Exception will be thrown?
If so, how do I parse lines that don't contain data in that position of the text line?