0

Case insensitive and if statements

My Code: ---------------------------------------------------------------------------------------------------- import java.util.Scanner; public class Codes { public static void main(String[] args) { Scanner read = new Scanner(System.in); System.out.println("Please enter your name: "); String name = read.nextLine(); System.out.println("Please specify your gender(M/F): "); String gender = read.nextLine(); if(gender == "m") { System.out.println("Welcome Mr." + name); } else if(gender == "f") { System.out.println("Welcome Ms." + name); } else { System.out.println("Invalid input. Please re-open the program to re-enter your information"); } } } ---------------------------------------------------------------------------------------------------- My question is how do I make it case insensitive and even though I enter the gender with lower-case, it only shows the "else" statement, why it doesn't work?

24th Jul 2022, 9:51 AM
Deniz
Deniz - avatar
1 Antwort
+ 2
Always you equals() method for string comparisons.. == compare references. equals() compares content. if( gender.equals("m") ) { And you have equalsIngoreCase() for case insensitive comparisions... Ex: if( gender.equalsIngoreCase("M") )
24th Jul 2022, 9:59 AM
Jayakrishna 🇮🇳