I have a comprehension problem right now. I have a csv table which has, among other things, the column “GESCHLECHT_HUND” with the possible entries “w” and “m” of type String.
When I program it like this it doesn’t work (all values are added to “weiteres” in the else-part):
Table table;
int weiblich = 0;
int maennlich = 0;
int weiteres = 0;
void setup() {
table = loadTable("20200306_hundehalter.csv", "header");
println(table.getRowCount() + " total rows in table");
for (int i = 0; i < table.getRowCount(); i++) {
TableRow row = table.getRow(i);
String geschlecht = row.getString("GESCHLECHT_HUND");
if (geschlecht == "w") {
weiblich++;
} else if (geschlecht == "m") {
maennlich++;
} else {
weiteres++;
}
}
println("");
print("Anzahl weibliche Hunde: "+weiblich+", Anzahl männliche Hunde: "+maennlich+", Weitere: "+weiteres);
}
But doing it like this works:
Table table;
int weiblich = 0;
int maennlich = 0;
int weiteres = 0;
void setup() {
table = loadTable("20200306_hundehalter.csv", "header");
println(table.getRowCount() + " total rows in table");
for (int i = 0; i < table.getRowCount(); i++) {
TableRow row = table.getRow(i);
String geschlecht = row.getString("GESCHLECHT_HUND");
if (int(geschlecht.charAt(0)) == 119) {
weiblich++;
} else if (int(geschlecht.charAt(0)) == 109) {
maennlich++;
} else {
weiteres++;
}
}
println("");
print("Anzahl weibliche Hunde: "+weiblich+", Anzahl männliche Hunde: "+maennlich+", Weitere: "+weiteres);
}
hi,
without seeing the csv file, everything looks fine to me,
i will consider to println("-"+ geschlecht+"-");
as charAt(0) works it looks like there is an extra character in your string…
otherwise, an easy ugly fix could be to use : if (geschlecht.charAt(0) == 'm') {
or if( geschlecht.startsWith("m"))
(note: in first one you compare a char ‘m’ and in second one a string"m" )