Hello i'm writing a simple username/password input from the user, but my code is bypassing all the if/ if else statements and going to the else statement. I'm not sure if my syntax is incorrect or whether using an if/else statement is incorrect. Or if I just coded wrong. Please help!
import java.util.Scanner;
public class Passwords
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String[] username = {"admin1", "admin2", "admin3", "admin4", "admin5"};
String[] password = {"pass1", "pass2", "pass3", "pass4", "pass5"};
System.out.println("Please enter in one of the following usernames : admin1, admin2, admin3, admin4, or admin5");
String userinput = input.nextLine();
System.out.println("Please enter in the password corresponding to your username: pass1, pass2, pass3, pass4, or pass5");
String passinput = input.nextLine();
if (userinput == username[0] && passinput == password[0])
{
System.out.println("Access granted!");
}
else if (userinput == username[1] && passinput == password[1])
{
System.out.println("Access granted!");
}
else if (userinput == username[2] && passinput == password[2])
{
System.out.println("Access granted!");
}
else if (userinput == username[3] && passinput == password[3])
{
System.out.println("Access granted!");
}
else if (userinput == username[4] && passinput == password[4])
{
System.out.println("Access granted!");
}
else
{
System.out.println("Sorry the username or password you entered is incorrect. Please try again");
System.out.println("Please enter in one of the following usernames : admin1, admin2, admin3, admin4, or admin5");
String userinput1 = input.next();
System.out.println("Please enter in the password corresponding to your username: pass1, pass2, pass3, pass4, or pass5");
String passinput1 = input.next();
if (userinput1 == username[0] && passinput1 == password[0])
{
System.out.println("Access granted!");
}
else if (userinput1 == username[1] && passinput1 == password[1])
{
System.out.println("Access granted!");
}
else if (userinput1 == username[2] && passinput1 == password[2])
{
System.out.println("Access granted!");
}
else if (userinput1 == username[3] && passinput1 == password[3])
{
System.out.println("Access granted!");
}
else if (userinput1 == username[4] && passinput1 == password[4])
{
System.out.println("Access granted!");
}
else
{
System.out.println("Sorry, your username/password does not match our database. Contact the administrator.");
}
}
}
}