1

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

    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Joey
  • 53
  • 4

3 Answers3

6
if(password.equals(passcheck)){
        pass = true;
    }

with == you compare the object references and not the values.

Furthermore you can simplify this:

if ((username == "" || username.length() < 5) || (username == " " || username.length() < 5)) {

to

if (username.trim().length() < 5) {
nano_nano
  • 12,351
  • 8
  • 55
  • 83
3

You have another issue in your loop beside wrong String comparison.

while (user != true && mail != true && pass != true)

means that the loop will terminate once any one of the three boolean flags is true.

What you want is probably

while (user != true || mail != true || pass != true)

which means the loop will only terminate when all three flags are true.

Or even better :

while (!user || !mail || !pass)
Eran
  • 387,369
  • 54
  • 702
  • 768
0

You are comparing Strings using == instead of equals.

Try using something like this:

"".equals(username) //...
Averroes
  • 4,168
  • 6
  • 50
  • 63