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.