I am trying to write a little program in Java that lets the user enter four Strings (username, email, password, passcheck).
The program is supposed to check if these strings fulfill certain conditions in a while
loop, which asks the user to enter the strings mentioned above, until they fulfill these conditions.
The program compiles and the first two if
cases seem to work fine. Only the comparisin between the two passwords (password
and passcheck
) seems to be wrong. Even if I enter the same String twice, they do not pass the comparison, and I have no idea why this is.
import java.util.Scanner;
public class Registry {
public static void main (String [] args){
Scanner scanner = new Scanner(System.in);
// Dialogue for the user to fill
System.out.println("Please enter your username (at least 5 characters)");
String username = scanner.nextLine();
System.out.println("Please enter a valid emailadress (an @ is necessary for it to pass.)");
String email = scanner.nextLine();
System.out.println("Please enter two matching passwords, so we can make sure that you entered them correctly.");
String password = scanner.nextLine();
String passcheck = scanner.nextLine();
boolean user = false;
boolean mail = false;
boolean pass = false;
// Error messages within a loop in order to make the user enter valid arguments.
while (user != true && mail != true && pass != true){
if ((username == "" || username.length() < 5) || (username == " " || username.length() < 5)) {
System.out.println("Username is empty or does not have enough characters! Please enter another.");
username = scanner.nextLine();
}
else{
user = true;
}
if (email.contains("@") == false){
System.out.println("Invalid Mailadress. Please enter again.");
email = scanner.nextLine();
}
else{
mail = true;
}
if(password == passcheck){
pass = true;
}
else if {
System.out.println("Your passwords do not match. Please enter them correctly.");
password = scanner.nextLine();
passcheck = scanner.nextLine();
}
}
}
}