0

when i enter the password "ahmad" the code doesn't execute as it true, i don't know what is the problem

but if i define variable pass = "ahmad" it is working very well

but i want to make the user enter the password by himself

public class main {

static Scanner uinput= new Scanner(System.in);

public static void main(String[] args) {


    String pass;

    while(true){

    System.out.print("Enter the password: ");
    pass= uinput.nextLine();

    if(pass== "ahmad"){
        System.out.println("It is working");
    }

    else System.out.println("failed");

    }

}

here is the console result:

Enter the password: ahmad failed Enter the password:

1 Answers1

0

when you compare Strings, you should use the equals method instead of ==. That should make it work.

Best, Julian

Julian
  • 1,694
  • 22
  • 29