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

**URL:** <https://discourse.processing.org/t/if-int-geschlecht-charat-0-119-works-but-if-geschlecht-m-not/30212>\
**Category:** Coding Questions\
**Created:** [May 21, 2021, 3:38pm UTC](https://discourse.processing.org/t/if-int-geschlecht-charat-0-119-works-but-if-geschlecht-m-not/30212 "2021-05-21T15:38:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joghurt](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Joghurt](https://discourse.processing.org/u/Joghurt)\
**Post date:** [May 21, 2021, 3:38pm UTC](https://discourse.processing.org/t/if-int-geschlecht-charat-0-119-works-but-if-geschlecht-m-not/30212/1 "2021-05-21T15:38:18Z")

</div>

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

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [May 21, 2021, 4:09pm UTC](https://discourse.processing.org/t/if-int-geschlecht-charat-0-119-works-but-if-geschlecht-m-not/30212/2 "2021-05-21T16:09:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Joghurt](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Joghurt](https://discourse.processing.org/u/Joghurt)\
**Post date:** [May 21, 2021, 4:15pm UTC](https://discourse.processing.org/t/if-int-geschlecht-charat-0-119-works-but-if-geschlecht-m-not/30212/3 "2021-05-21T16:15:43Z")

</div>

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](https://opendata.swiss/de/dataset/hundebestand-der-stadt-zurich2/resource/3e9d8bb5-25b7-491b-a723-2dee5dd1bae3)

That

```auto
if( geschlecht.startsWith("m"))

```

works and

```auto
if( geschlecht == "m")

```

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 21, 2021, 7:05pm UTC](https://discourse.processing.org/t/if-int-geschlecht-charat-0-119-works-but-if-geschlecht-m-not/30212/4 "2021-05-21T19:05:03Z")

</div>

> [@Joghurt](#):
>
> ` if (geschlecht == "w") {`

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

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

```

---

<div class="post-metadata">

**Author:** ![Joghurt](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Joghurt](https://discourse.processing.org/u/Joghurt)\
**Post date:** [May 21, 2021, 7:28pm UTC](https://discourse.processing.org/t/if-int-geschlecht-charat-0-119-works-but-if-geschlecht-m-not/30212/5 "2021-05-21T19:28:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [May 21, 2021, 9:44pm UTC](https://discourse.processing.org/t/if-int-geschlecht-charat-0-119-works-but-if-geschlecht-m-not/30212/6 "2021-05-21T21:44:53Z")

</div>

Hi Chrisir,

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

but testing this:

```auto
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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 22, 2021, 1:11pm UTC](https://discourse.processing.org/t/if-int-geschlecht-charat-0-119-works-but-if-geschlecht-m-not/30212/7 "2021-05-22T13:11:28Z")

</div>

I can’t really answer that

Has something to do with some magic under the hood

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 22, 2021, 1:16pm UTC](https://discourse.processing.org/t/if-int-geschlecht-charat-0-119-works-but-if-geschlecht-m-not/30212/8 "2021-05-22T13:16:52Z")

</div>

Hello,

> [@th75](#):
>
> do you know what are the rules?

I have come across this in the past.

Check these out:

- [String Comparison in Java - javatpoint](https://www.javatpoint.com/string-comparison-in-java)
- [Compare two Strings in Java - GeeksforGeeks](https://www.geeksforgeeks.org/compare-two-strings-in-java/#:~:text=Using%20String.,match%2C%20then%20it%20returns%20false).

`:)`
