0

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.");
                }
            }
        }
}
user3431662
  • 1
  • 1
  • 1

3 Answers3

1

use, userinput.equals(username[0]) //This will compare the String values

In java == is going to compare the object id's and if they are different objects it will be always false.

Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
RKC
  • 1,834
  • 13
  • 13
0

In Java, for string comparison string1.equals(string2) is being used.

Dipika
  • 584
  • 2
  • 12
0

use equals() method of String class for comparison as

userinput == username[0] to userinput.equals(username[0])


== tests for reference equality.

.equals() tests for value equality.

Ex:

new String(userinput).equals(username[0]) // --> true 

new String(userinput) == username[0] // --> false 

new String(userinput) == new String(username[0]) // --> false 
Arjit
  • 3,290
  • 1
  • 17
  • 18