lundi 17 août 2020

converting to uppercase but the if statement not reading it [duplicate]

I am fairly new to Java but have come upon a basic problem. So when I get the user to input a string into the console, I convert it to uppercase. But then when I use an if statement to check if what the user entered into the console matches the condition it doesn't match. So say if I enter "Mark21" it converts to the uppercase some reason goes the the else statement.

Scanner scanner = new Scanner(System.in);
    System.out.println("Enter username");
    String val1 = scanner.next();

    val1.toUpperCase();

    if (val1 == "MARK21"){
        System.out.println("username accepted");
    }
    else{
        System.out.println("username not accepted");
    }

Aucun commentaire:

Enregistrer un commentaire