Por qué el substring en un if siempre me devuelve falso? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Por qué el substring en un if siempre me devuelve falso?

String x="hello"; if(x.substring(0, 1)=="h") { System.out.println("pepe"); }else{ System.out.println("juan"); } // why ouput is Juan? https://code.sololearn.com/cdRGae4qOL5j/?ref=app

23rd Jun 2020, 11:35 PM
Adrián
Adrián - avatar
4 Answers
+ 4
Use .equal() not == for String comparison if(x.substring(0, 1).equals("h"))
23rd Jun 2020, 11:59 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Yo no hablo mucho español pero puedo usar el traductor de Google
24th Jun 2020, 12:14 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Muchas gracias me ayudaste muchísimo, no te imaginas cuánto😅(Do you understand me?)
24th Jun 2020, 12:04 AM
Adrián
Adrián - avatar
+ 1
😅Bueno, muchas gracias
24th Jun 2020, 12:15 AM
Adrián
Adrián - avatar