This question already has an answer here:
- How do I compare strings in Java? 23 answers
The problem is that the variables "check" and "password" are both the same string, but the if statement says it's false for some reason. How do i fix this exactly?
Here's the code:
import java.util.Scanner;
public class Manager {
public static Scanner input = new Scanner(System.in);
public static boolean isPasswordSet;
public static String password;
public static void password() {
String check;
while (true) {
if (isPasswordSet == false) {
System.out.println("Please set a new password: ");
password = input.next();
System.out.println("Enter password again: ");
check = input.next();
if (check == password) {
isPasswordSet = true;
System.out.println("Welcome");
return;
} else if (check != password) {
System.out.println("Password incorrect.");
isPasswordSet = false;
continue;
}
} else if (isPasswordSet == true) {
System.out.println("Please enter password: ");
check = input.next();
if (check == password) {
System.out.println("Welcome");
} else if (check != password) {
System.out.println("Password incorrect.");
continue;
}
}
}
}
public static void main(String args[]) {
password();
}
}
Output:
Please set a new password:
ryan
Enter password again:
ryan
Password incorrect.
Please set a new password:
Aucun commentaire:
Enregistrer un commentaire