dimanche 12 août 2018

Java returns false when comparing substrings with same Char [duplicate]

This question already has an answer here:

I'm having the problem, that i'm comparing 3 substrings

 a.substring(0,1) 
 a.substring(1,2)
 and
 a.substring(2,3)  

and I set a = "XXX" and when I print the substrings, they all say X but when i compare them like this:

 if(a.substring(0,1) == a.substring(1, 2) & a.substring(1, 2) == a.substring(2, 3)) {
     System.out.println("Hello");
 }

it does nothing. I checked if I'm using the If clause wrong but when I tested this:

 System.out.println(a.substring(0,1) == a.substring(1,2));

It returned false too.

I'm currently using Java in Eclipse and I even defined my a variable manually instead of using my function, I planned on using before, for testing.

I'm happy if anyone could help

Aucun commentaire:

Enregistrer un commentaire