In addition to Ibz' detailed answer (+1) explaining manual parsing, here's a version using Processing's Table class which can be loaded using loadTable():
Table table;
void setup() {
// load table and specify the first row is a header
table = loadTable("Airpollution.csv", "header");
int rowCount = table.getRowCount();
// for each row
for (int i = 0; i < rowCount; i++) {
// access the row
TableRow row = table.getRow(i);
// access each piece of data by column name...
String place = row.getString("place");
float no2 = row.getFloat("NO2(ppm)");
float co = row.getFloat("CO(ppm)");
// ...or column index
int airPollution = row.getInt(3);
// print the data
println(i, place, no2, co, airPollution);
}
}
Now that you can access the parsed data, you can plug it to instances AirPollution:
Table table;
void setup() {
// load table and specify the first row is the CSV header
table = loadTable("Airpollution.csv", "header");
int rowCount = table.getRowCount();
// use row count (as .csv data could change)
AirPollution[] AP = new AirPollution[rowCount];
// for each row
for (int i = 0; i < rowCount; i++) {
// access the row
TableRow row = table.getRow(i);
// initialize AirPollution data with row data
AP[i] = new AirPollution(row.getString("place"),
row.getFloat("NO2(ppm)"),
row.getFloat("CO(ppm)"),
row.getInt(3));
// print the data
println("row index",i,"data",AP[i]);
}
}
class AirPollution {
String place;
float NO2;
float CO;
int AP;
AirPollution(String p, float x, float y, int c) {
place=p;
NO2=x;
CO=y;
AP=c;
}
// display nicely when passing this instance to print/println
String toString(){
// %.3f = floating point value with 3 decimal places
return String.format("{ place=%s, NO2=%.3f, CO=%.3f, AP=%d} ", place, NO2, CO, AP);
}
}
Notice I've renamed the file from .txt to .csv: this extension might help preview/test the data using OpenOffice Calc, Excel, Google Sheets, etc.
Have fun visualising the data.
If you want to learn more about the Table class, other the reference, also check out Processing > Examples > Topics > Advanced Data > LoadSaveTable