Splitting .csv files.
in
Programming Questions
•
10 months ago
Hey guys, I'm very new to processing and programming as a whole and wondered if you could tell me why my code isn't working. I have to read in lines of a csv file, and split it into pieces. Hopefully you can understand. It will print lines, however not pieces or records. The output is:
Mozambique,47,23390765,12,50
hmm$Record@359ecd80 <----- This is wrong
Thanks
Record[] records;
String[] lines;
int recordCount;
String[] pieces;
void setup(){
lines = loadStrings("main.csv");
records = new Record[lines.length];
for (int i = 0; i < lines.length; i++){
String[] pieces = split(lines[i], ",");
if (pieces.length == 5){
records[recordCount] = new Record(pieces);
recordCount++;
}
}
if (recordCount != records.length){
records = (Record[]) subset(records, 0, recordCount);
}
println(lines[0]);
println(records[0]);
}
class Record{
private String name;
private int water;
private int pop;
private int wealth;
private int life;
public Record(String[] pieces){
name = pieces[0];
water = int(pieces[1]);;
pop = int(pieces[2]);;
wealth = int(pieces[3]);
life = int(pieces[4]);
}
String getName(){
return name;
}
int getWater(){
return water;
}
int getPop(){
return pop;
}
int getwealth(){
return wealth;
}
int getLife(){
return life;
}
}
1