So i'm just doing some elementary testing while I learn java, and i'm curious why this doesn't work:
public static void main(String[] args) {
String[] names={"John","Mark","Suzy","Anthony"};
String[] passwords={"passw0rd","CandyC4ne","x5021","ADawg55"};
System.out.println("Please indicate which user you would like to log in as:\n(1)John\n(2)Mark\n(3)Suzy\n(4)Anthony");
Scanner x=new Scanner(System.in);
int y=x.nextInt()-1;
System.out.print("Please enter the password to log into "+names[y]+"'s account: ");
Scanner a=new Scanner(System.in);
String newpass=a.nextLine();
System.out.println(passwords[y]);
if(passwords[y]==newpass){
System.out.print("Hello "+names[y]+"!");
}else{
System.out.println("Incorrect Password. "+passwords[y]+" "+newpass);
}
}
Both passwords[y] and newpass are the exact same string, and when I try:
if(passwords[y]=="passw0rd");
(And obviously pick the 'John') it does work, so i'm hoping someone can explain the difference and how to get around it so as to better accept user input.
Also i'll take a for loop as an answer, but i'm hoping I can avoid it.