vendredi 20 novembre 2015

One of three if - cases in a while loop does not seem to work, while the other two do?

it's been a while. Haven't written a line of code in the last few months, so please be gentle.

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 comparision 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();
    }
}

}
}

Aucun commentaire:

Enregistrer un commentaire