0

i have a question for you about java program in netbeans. I made this login program in java netbeans project, but i have some problem here. Can any one help me to find any mistakes in my algoritm? --first, there is my script below :

public static void main(String[] args) {

    int i;
    int x;
    int amountOfData=0;
    String user="";
    String pass="";
    String[] username = new String[100];
    String[] password = new String[100];
    boolean found=false;

    Scanner sc = new Scanner(System.in);

    i=0;
    while(i<3){
        System.out.print("Input username -"+(i+1)+":");
        username[i] = sc.nextLine();
        System.out.print("Input password -"+(i+1)+":");
        password[i] = sc.nextLine();
        System.out.println("");
        i++;
        amountOfData++;
    }

    System.out.println("============================");
    System.out.println("Welcome in Login Form");
    System.out.println("============================");
    for(x=1;x<=3;x++){
        System.out.print("Username :");
        user = sc.nextLine();
        System.out.print("Password :");
        pass = sc.nextLine();
        i=0;
        while(i<amountOfData && found==false){
            **if(user==username[i] && pass==password[i])
                found=true;**
            else{
                System.out.print("haha");
                i++;
            }
        }
        if(found==true){
            System.out.println("You Succesfully Login");
            break;
        }
        else{
            System.out.println("Error ! Please Try again !");
        }
    }

} 

When i running this program, the output is :

Input username-1 :a Input password-1 :1

Input username-2 :a Input password-2 :2

Input username-3 :a Input password-3 :3

======================

Welcome in Login Form

======================

Username :a

Password :3

haha

haha

haha

Error ! Try again enter login data !

Username :

as you have seen above, the username and password that i entered for login is true, that's (a,3). But somehow, the function if(user==username[i] && pass==password[i]) can't executed. So, the program is executed the else function 3 times and output "haha". Any answer how this can be happened?? I need the answer immediately... Thank you.

2 Answers2

0

In Java, using == tests whether the references are equal (same object) where as .equals() tests for whether the values are equal.

As a result when you want to test whether two strings have the same value, you want to use .equals() and not ==.

maldahleh
  • 313
  • 3
  • 18
0

Your issue has to do with the fact that you're using == or != with different String objects. In order to correct your program, you would want to change this : if(user==username[i] && pass==password[i]) into something like this: if(user.equals(username[i]) && pass.equals(password[i])). This has to do with the fact that == or != compares objects based not on the arbitrary values with which you assign to them, but rather, the object itself.

JamesM
  • 13
  • 5