w3resource

Java Method Exercises: Check whether a string is a valid password

Java Method: Exercise-11 with Solution

Write a Java method to check whether a string is a valid password.

Password rules:
A password must have at least ten characters.
A password consists of only letters and digits.
A password must contain at least two digits.

Sample Solution:

Java Code:

import java.util.Scanner;

public class Exercise11 {
    
public static final int PASSWORD_LENGTH = 8;

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print(
                "1. A password must have at least eight characters.\n" +
                "2. A password consists of only letters and digits.\n" +
                "3. A password must contain at least two digits \n" +
                "Input a password (You are agreeing to the above Terms and Conditions.): ");
        String s = input.nextLine();

        if (is_Valid_Password(s)) {
            System.out.println("Password is valid: " + s);
        } else {
            System.out.println("Not a valid password: " + s);
        }

    }

    public static boolean is_Valid_Password(String password) {

        if (password.length() < PASSWORD_LENGTH) return false;

        int charCount = 0;
        int numCount = 0;
        for (int i = 0; i < password.length(); i++) {

            char ch = password.charAt(i);

            if (is_Numeric(ch)) numCount++;
            else if (is_Letter(ch)) charCount++;
            else return false;
        }


        return (charCount >= 2 && numCount >= 2);
    }

    public static boolean is_Letter(char ch) {
        ch = Character.toUpperCase(ch);
        return (ch >= 'A' && ch <= 'Z');
    }


    public static boolean is_Numeric(char ch) {

        return (ch >= '0' && ch <= '9');
    }

}

Sample Output:

1. A password must have at least eight characters.                                                            
2. A password consists of only letters and digits.                                                            
3. A password must contain at least two digits                                                                
Input a password (You are agreeing to the above Terms and Conditions.): abcd1234                              
Password is valid: abcd1234 

Flowchart:

Flowchart: Check whether a string is a valid password

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java method to check whether an year (integer) entered by the user is a leap year or not.
Next: Write a Java method (takes a number n as input) to displays an n-by-n matrix.

What is the difficulty level of this exercise?



Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH