1
I run this code and the if
/else if
are not entering. What can be?
import java.util.Scanner;
public class VerificaLetra {
public static void main(String[] args) {
String sexo = " ";
String letra;
Scanner scan;
System.out.print("Informe o sexo. F para feminino e M "+
"para Masculino: ");
scan = new Scanner(System.in);
letra = scan.nextLine();
if (letra == "f" || letra == "F") {
sexo = "Sexo = Feminino";
}
else if (letra == "m" || letra == "M") {
sexo = "Sexo = Masculino";
}
System.out.println(sexo);
}
}
It’s because strings are objects, you need to use
letra.equals("m")
. There are several questions here about that, I marked yours as a duplicate of one of them.– bfavaretto
Thanks! I’m new to the site, but I’ll try to search before the next one. I will test your answer, the topic you sent also has some cool answers!
– Gabriel Saldanha
It worked. I read the topic and managed to understand the concept well. Thank you!
– Gabriel Saldanha