Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Password_Validator {
- public static void main(String[] args) throws IOException {
- BufferedReader reader =
- new BufferedReader(
- new InputStreamReader(System.in));
- System.out.print("Enter password: ");
- String password = reader.readLine();
- if(!hasSixToTenDigits(password)) {
- System.out.println("The password mast be between six and ten symbols!");
- }
- if (!hasOnlyLettersAndDigits(password)) {
- System.out.println("The password must consists of letters and digits, only!");
- }
- if (!hasAtLeastTwoDigits(password)) {
- System.out.println("The password must have at least two digits!");
- }
- if(hasAtLeastTwoDigits(password) && hasOnlyLettersAndDigits(password) &&
- hasSixToTenDigits(password)){
- System.out.println("Valid Password!");
- }
- }
- private static boolean hasSixToTenDigits(String password) {
- if (password.length() >= 6 && password.length() <= 10) {
- return true;
- }
- return false;
- }
- private static boolean hasOnlyLettersAndDigits(String password) {
- for (int i = 0; i < password.length(); i++) {
- if (!Character.isLetterOrDigit(password.charAt(i))) {
- return false;
- }
- }
- return true;
- }
- private static boolean hasAtLeastTwoDigits(String password) {
- int count = 0;
- for (int i = 0; i < password.length(); i++) {
- if (Character.isDigit(password.charAt(i))) {
- count ++;
- }
- }
- if (count < 2){
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement