Steve
Steve

Reputation: 49

Java Password searching for characters

I have to create a program that creates a valid password, the requirements for this password are: must have between 5 and 8 characters, must contain one number, one uppercase letter and one lowercase letter. It also has to be in the form of a class and a test program but right now im just trying to get the program to work correctly. As it is now, the length checks, and the uppercase checks work fine its the number check thats giving me problems. Its saying the password is invalid every time I put a number in it. Heres what i have so far.

import java.util.Scanner;

public class pass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String password;
        int length;
        boolean hasUppercase;
        boolean hasLowercase;
        boolean hasNum;

        do {
            System.out.println("Password must be between 5 and 8 characters 
            and must contain a lowercase letter, an uppercase letter, and a 
            number");
            System.out.println("Please enter your password: ");
            password = sc.next();
            length = password.length();
            hasUppercase = !password.equals(password.toLowerCase());
            hasLowercase = !password.equals(password.toUpperCase());
            hasNum = !password.matches(".*\\d+.*");


            if(length >= 5 & length <= 8 ) {
                if(hasUppercase & hasLowercase & hasNum) {
                    System.out.print("Your password has been set");

                }
            }
            else {
                System.out.print("Password must be between 5 and 8 
                characters and must contain a lowercase letter, an uppercase 
                letter, and a number");
            }

        }
        while(length <= 5 | length >= 8 | !hasUppercase | !hasLowercase | 
!hasNum);
    }
}

Upvotes: 0

Views: 204

Answers (3)

John3136
John3136

Reputation: 29266

hasNum = !password.matches(".*\\d+.*"); says "hasNum = true if the string DOES NOT contain a digit" - just remove the !

I'd also simplify the end condition - one single boolean "passwordOk" rather than checking all 5 conditions again.

Upvotes: 1

Mohit Mutha
Mohit Mutha

Reputation: 3001

Edited your program. Changes documented inline

public class pass {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String password;
    int length;
    boolean hasUppercase;
    boolean hasLowercase;
    boolean hasNum;

    do {
        System.out.println("Password must be between 5 and 8 characters and must contain a lowercase letter, an uppercase letter, and a number");
        System.out.println("Please enter your password: ");
        password = sc.next();
        length = password.length();
        hasUppercase = !password.equals(password.toLowerCase());
        hasLowercase = !password.equals(password.toUpperCase());
        hasNum = !password.matches(".*\\\\d+.*");

        if(length >= 5 & length <= 8 ) {
            if(hasUppercase & hasLowercase & hasNum) {
                System.out.print("Your password has been set");

            }
        }
        else {
            System.out.print("Password must be between 5 and 8  characters and must contain a lowercase letter, an uppercase letter, and a number");
        }

    }
    while(length < 5 | length > 8 | !hasUppercase | !hasLowercase | !hasNum); //Removed >= and <= for length checks
}

}

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

You could make your life much easier and just use a regular expression for this:

String password = "Hello123";
if (password.matches("(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z]).{5,8}")) {
    System.out.println("Password is valid");
}
else {
    System.out.println("Password is invalid.");
}

Demo

Upvotes: 0

Related Questions