PAT(B) 1081 check password (Java)

Title Link: 1081 check password (15 point(s))

Title Description

This question asks you to help the user registration module of a website write a small function of password validity check. The website requires that the password set by the user must be composed of no less than 6 characters, and can only have English letters, numbers and decimal points, as well as both letters and numbers.

Input format

Enter the first line to give a positive integer N (≤ 100), then N lines, each line to give a password set by the user, which is a non empty string of no more than 80 characters, ending with carriage return.

Output format

For each user's password, the system feedback information is output in one line, which is divided into the following five types:

  • If the password is legal, output Your password is wan mei.
  • If the password is too short, whether it is legal or not, output Your password is tai duan le.;
  • If the password length is legal, but there are illegal characters, then output Your password is tai luan le.;
  • If the password length is legal, but only letters do not have numbers, then output Your password needs shu zi.;
  • If the password length is legal, but only numbers have no letters, then output Your password needs zi mu.

sample input

5
123s
zheshi.wodepw
1234.5678
WanMei23333
pass*word.6

sample output

Your password is tai duan le.
Your password needs shu zi.
Your password needs zi mu.
Your password is wan mei.
Your password is tai luan le.

Analysis

  • The password may have spaces, so you cannot use next(), you should use nextLine().

Java code

/*********************************************************************************
Submit Time			Status		Score	Problem	Compiler		Run Time	User
7/24/2019, 19:49:03	Accepted	15		1081	Java (openjdk)	105 ms		wowpH
*********************************************************************************/
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < N; ++i) {
            char[] password = sc.nextLine().toCharArray();
            if (password.length < 6) {
                System.out.println("Your password is tai duan le.");
                continue;
            }
            boolean letter = false, digital = false, other = false;
            for (int j = 0; j < password.length; ++j) {
                char ch = password[j];
                if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
                    letter = true;
                } else if (ch >= '0' && ch <= '9') {
                    digital = true;
                } else if ('.' != ch) {
                    other = true;
                }
            }
            if (true == other || false == letter && false == digital) {
                System.out.println("Your password is tai luan le.");
            } else if (true == letter && false == digital) {
                System.out.println("Your password needs shu zi.");
            } else if (false == letter && true == digital) {
                System.out.println("Your password needs zi mu.");
            } else {
                System.out.println("Your password is wan mei.");
            }
        }
        sc.close();
    }
}

Keywords: Java less

Added by dhruvasagar on Wed, 16 Oct 2019 20:42:45 +0300