If (int(geschlecht.charAt(0)) == 119) works, but if (geschlecht == "m") not

Hi

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);
}

What is wrong with the first programm?

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" )

1 Like

Thanks. All your ideas work well (I like the second one). But I still cant’t understand, why the first programm doesn’t work. Here is where you can download the csv: https://opendata.swiss/de/dataset/hundebestand-der-stadt-zurich2/resource/3e9d8bb5-25b7-491b-a723-2dee5dd1bae3

That

if( geschlecht.startsWith("m"))

works and

if( geschlecht == "m")

doesn’t work would mean, that there is “something” after the “m”. But

if(int( geschlecht.charAt(1)) == 109)

throws an error, so there isn’t “something”. Ok, I have a working solution, but it would really interest me, where the error is in the first program.

that == won’t work for String

say if(geschlecht.equals("w")) {

also String is “m” …

and char is ‘m’ …

to check a char would work with == but you should use ‘A’ instead of “A”

REMARK

you don’t need int() with char by the way

String geschlecht = "m"; 

void setup() {
  size(300, 300);
  background(0);
}

void draw() {
  background(0); 
  println( geschlecht.charAt(0)   );

  // no int() needed 
  if (geschlecht.charAt(0) == 109) 
    text("Yes", 19, 19);

  if (geschlecht.equals("m")) 
    text("Yes", 19, 59);
}

1 Like

Ahhhh, right. Thank you both, Chrisir and th75!

1 Like

Hi Chrisir,

i thought about equals() as i learn is how to properly compare string,

but testing this:

String s="m";
if(s=="m")println("yes");

worked as expected (on my mac)
do you know what are the rules? when or why it works or fail?
Thanks

I can’t really answer that

Has something to do with some magic under the hood

1 Like

Hello,

I have come across this in the past.

Check these out:

:)

1 Like