Skip to content

Instantly share code, notes, and snippets.

@macklinu
Last active October 12, 2015 07:58
Show Gist options
  • Save macklinu/3995855 to your computer and use it in GitHub Desktop.
Save macklinu/3995855 to your computer and use it in GitHub Desktop.
CSV class Processing
//A class created for CSV parsing based on...
//for importing csv files into a 2d array
//by che-wei wang
class CSV {
String dataFile;
String lines[];
String [][] csv;
int csvWidth;
CSV() {
csvWidth = 0;
}
void loadFile(dataFile) {
lines[] = loadStrings(dataFile);
}
void parse(String delimiter) {
//calculate max width of csv file
for (int i=0; i < lines.length; i++) {
String [] chars=split(lines[i], delimiter);
if (chars.length>csvWidth) {
csvWidth=chars.length;
}
}
//create csv array based on # of rows and columns in csv file
csv = new String [lines.length][csvWidth];
//parse values into 2d array
for (int i=0; i < lines.length; i++) {
String [] temp = new String [lines.length];
temp= split(lines[i], delimiter);
for (int j=0; j < temp.length; j++) {
csv[i][j]=temp[j];
}
}
}
String value(int i, int j) {
return csv[i][j];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment