1

okay so i'm making a password checker program and there's a few rules. the password must be 8 or more characters the password must contain 2 or more digits the password can only be letters and numbers here is what i have so far how do i check for 2 digits and for only letters and numbers? thanks

import java.util.Scanner;

public class checkPassword {

    public static boolean passwordLength(String password) {
        boolean correct = true;
        int digit = 0; 

        if (password.length() < 8) {
            correct = false;
        }
        return correct;
    }


    public static void main(String[] args) {
        // Nikki Kulyk


        //Declare the variables
        String password;

        Scanner input = new Scanner(System.in);

        //Welcome the user
        System.out.println("Welcome to the password checker!");

        //Ask the user for input
        System.out.println("Here are some rules for the password because we like to be complicated:\n");
        System.out.println("A password must contain at least eight characters.\n" +
                "A password consists of only letters and digits.\n" +
                "A password must contain at least two digits.\n");
        System.out.println("Enter the password: ");
        password = input.nextLine();

        boolean correct = passwordLength(password);


        if (correct) {
            System.out.println("Your password is valid.");
        }
        else {
            System.out.println("Your password is invalid.");
        }

    }
}
Steephen
  • 14,645
  • 7
  • 40
  • 47
Nicole
  • 5
  • 1
  • 6
  • check this [link](http://codereview.stackexchange.com/questions/63283/password-validation-in-java) You will find some validation methods. – Preshan Pradeepa Nov 06 '15 at 03:21
  • thank you, but i'm looking for an actual explanation, not just a code because i'm a beginner programmer and i do not fully understand what all of that stuff means. – Nicole Nov 06 '15 at 03:32

5 Answers5

0

Start reading and using pattern, regex in java. Below link has good explanation, you can find lots of examples.I hope this is what you are looking for to solve your problem. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

bakki
  • 314
  • 3
  • 10
  • works but gets super ugly: http://stackoverflow.com/questions/5142103/regex-for-password-strength - would simply check criteria with a for loop. – zapl Nov 06 '15 at 03:45
  • @zapl Did my answer is incorrect or inappropriate. please help me understand if so – bakki Nov 06 '15 at 03:49
  • No, your answer is ok :) It's just complicated to create the correct regular expression. – zapl Nov 06 '15 at 03:54
  • ok.Thanks for the clarification. zapl intended to learn concept – bakki Nov 06 '15 at 04:00
0

I was going to suggest regular expressions too but as a new programmer they may be difficult to understand. A good starting approach would be to convert the password into an array of characters and then make use of Character.isLetterOrDigit(c) and count the number of digits.

Mark Sholund
  • 1,273
  • 2
  • 18
  • 32
0

Regex is the weapon of choice, with which the solution reduces to a single method call:

boolean correct = password.matches("(?=(.*\\d){2})[a-zA-Z0-9]{8,}");
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Here is a solution. It is not efficient, however it should be helpful for beginners. It breaks the problem into steps. Each requirement for the password is tested one step at a time. If at any point, one of the requirements are violated the method returns false; there is no point in making more checks once we know for sure the password is invalid.

The method first checks for the length (as above). Then it uses a for loop to count the number of digits in the String. Next it uses a regular expression to ensure that only alphanumeric characters are in the password. Notice the logical negation (!) operator when testing if the String is alphanumeric. It enables a return of false if the String does not contain exclusively letters and digits.

Comments are included to illustrate and clarify the logic. Good luck.

public static boolean passwordLength(String password) {
    /* Declare a boolean variable to hold the result of the method */
    boolean correct = true;

    /* Declare an int variable to hold the count of each digit */
    int digit = 0; 

    if (password.length() < 8) {
        /* The password is less than 8 characters, return false */
        return false;
    }

    /* Declare a char variable to hold each element of the String */
    char element;

    /* Check if the password has 2 or more digits */
    for(int index = 0; index < password.length(); index++ ){

        /* Check each char in the String */
        element = password.charAt( index );

        /* Check if it is a digit or not */
        if( Character.isDigit(element) ){
            /* It is a digit, so increment digit */
            digit++;
        } // End if block

    } // End for loop 

    /* Now check for the count of digits in the password */
    if( digit < 2 ){
        /* There are fewer than 2 digits in the password, return false */
        return false;
    }

    /* Use a regular expression (regex) to check for only letters and numbers */
    /* The regex will check for upper and lower case letters and digits */
    if( !password.matches("[a-zA-Z0-9]+") ){
        /* A non-alphanumeric character was found, return false */
        return false;
    }

    /* All checks at this point have passed, the password is valid */
    return correct;

}
Aeros
  • 71
  • 3
0
   public boolean isPassOk(String val)
{
   boolean isUp       = false;
   boolean isDown     = false;
   boolean isNum      = false;
   boolean isSPecial  = false;

   char c[] = val.toCharArray();

   for(int k = 0; k<c.length; k++)
   {
       int cc = (int)c[k];

       if(cc >= 48 && cc <= 57 )  {isNum = true;} else
       if(cc >= 65 && cc <= 90 )  {isUp = true;} else
       if(cc >= 97 && cc <= 122 ) {isDown = true;} else
           isSPecial = true;


   }

   if(isDown && isUp && isNum && isSPecial) return true;

   return false;
}
Blazej Kita
  • 99
  • 1
  • 1
  • 10