0

I know this is something really small and stupid but I don't know why this doesn't work. Basically there are only two names of employees that can be used to login. These two names are stored in an array. Once the user enters a name and it's not one of the two correct ones, the program keeps asking for the user name.

String [] userNames = {"Jack", "Jill"};  
logIn(userNames);

public static void logIn(String [] name)
{
    String userName = "";
    Scanner kb = new Scanner(System.in);
    System.out.print("Enter Username: ");
    userName = kb.nextLine();
    while (userName != name[0] || userName != name[1])
    {
        System.out.print("Enter Username: ");
        userName = kb.nextLine();
    }
    System.out.println("Correct");
}
user3466181
  • 3
  • 1
  • 3

1 Answers1

1

To compare strings, equals or compareTo should be used:

String [] userNames = {"Jack", "Jill"};  
logIn(userNames);

public static void logIn(String [] name)
{
    String userName = "";
    Scanner kb = new Scanner(System.in);
    System.out.print("Enter Username: ");
    userName = kb.nextLine();
    //while (userName != name[0] || userName != name[1])
    while (!userName.equals(name[0]) && !userName.equals(name[1]))
    {
        System.out.print("Enter Username: ");
        userName = kb.nextLine();
    }
    System.out.println("Correct");
}
alain
  • 11,939
  • 2
  • 31
  • 51