allencoded
allencoded

Reputation: 7275

Java code Check Password for Letter and Digit?

The first post was confusamagin. My assignment is to create a password prompt program. The password needs to be checked to see if it does have at least one digit and one letter in it. Also the password length must be between 6 - 10.

My problem is trying to figure out how see if a digit and letter exist the password. In the check password area I am not sure where to begin really. I am not sure how to see if it has a Letter and a Digit in one. I know how to do either or by using a for statement to count and check but all it does is check to see rather it contains all letters or all digits.

import java.util.Scanner;

class Password {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

//------ENTER A USERNAME
        System.out.println("Welcome please enter your username and password.");
        System.out.print("Username >>");
        input.nextLine();


//------PASSWORD AUTHENTICATION BEGIN       
        String password = enterPassword();
            while ( !checkPassword(password) ) {
            System.out.println("Password must be 6 - 10 characters long!"); 
            password = enterPassword();
            }

//------PASSWORD VERIFY
        String passwordverify = enterPassword();
        while (!password.equals(passwordverify)){
            System.out.println("ERROR - Passwords DO NOT MATCH Re-Enter Passwords Again");
            password = enterPassword();

        }

//------ACCEPT PASSWORD     
        System.out.println("Username and Password Accepted!");

        }


//--ENTER PASSWORD STATEMENT
    public static String enterPassword(){
        String password;
        Scanner input = new Scanner(System.in);
        System.out.print("Password >>");
        password = input.nextLine();
        return password;
        }

//--BOOLEAN CHECK PW    
    public static boolean checkPassword(String password){
        int length;
        length = password.length();
            if (length < 6 || length > 11){
            return false;
            }
        for (int i = 0; i < password.length();i++){
            if (!Character.isLetter(password.charAt(i)))
            return false;
        }
        return true;
        }

}

Upvotes: 0

Views: 8719

Answers (2)

fsalim
fsalim

Reputation: 13

Your code is only missing variable counters. Keep two counters: one to count letters, one to count numbers, and perform the if test at the end of both for loops inside checkPassword to check if both counters are at least one.

An alternative of doing this is using regular expression. See this link: http://www.mkyong.com/regular-expressions/how-to-validate-password-with-regular-expression/

Upvotes: 1

Jim Barrows
Jim Barrows

Reputation: 3634

Without writing your homework assignment.... you're on the right track you have make sure that your count of letters, and your count of numbers are both at least 1.

Upvotes: 1

Related Questions