2

I'm a student of CIS taking a Java class (hoping to become a programmer). Anyhow, I have an assignment to create a program that checks a password for validity. If the password is not valid, it is to print a message. If the password is valid, the user is to reinsert the password. If they both match, it is to print a message accepting. If they do not match, it is to print a message. The password requirements are:

  • Must be at least 8 characters in length
    1. Must contain one number
    2. Must contain one letter
    3. Must contain one special character
    4. Cannot have 3 or more identical characters
    5. Cannot contain spaces
    6. Cannot start with ! or ?

I am using netbeans.

package pass1;

import java.util.Scanner;

public class Pass1 {

//Main method asks for pasword from user and calls upon methods to verify                                 password requirements and password 1 and 2 match
public static void main(String[] args) {
    //declare variables
    String pass1;
    String pass2;
    boolean passvalid;
    boolean passmatch;


//initialize new instance of scanner
Scanner kb = new Scanner(System.in);
//get password
System.out.println("Please input password. (Must be at least 8 characters,  contain a number, letter, and special character");
pass1 = kb.nextLine();
//initialize instance of passisvalid to check password for requirements
passvalid = passisvalid(pass1);
//if pass is valid, allow user to reinsert password 
if (passvalid){
System.out.println("Please reinsert password:");
pass2 = kb.nextLine();
//initialize instance of passismatch to check passwords match
passmatch = passismatch(pass1, pass2);
//if passwords do not match, print message
if (!passmatch)
{
System.out.println("Passwords do not match.");
}
//if passwords match, print message
else if (passmatch)
{
    System.out.println("Password set.");
}
}
//if password is not valid, print message         
else
{
    System.out.println("Password is not valid:");
}


}

     /*************************************************************************************/   

//this method check that user inputted password meets requirements, and         returns boolean value 
public static boolean passisvalid(String password) {
//declare variables
boolean letter;
boolean digit;
boolean space;
boolean length;
boolean start1;
boolean start2;
boolean valid;
boolean special;
//initialize variables
valid=false;
letter=false;
digit=false;
space=false;
special=false;
length = false;
start1=false;
start2=false;

//initialize count 
for(int i=0;i<password.length();i++)
{
char s=password.charAt(i);
//check for letter in password
if(Character.isLetter(s))
{
letter = true;
}
//check for number in password
if(Character.isDigit(s))
{
digit = true;
}
//check for spaces in password
if(Character.isSpaceChar(s))
{
space=true;
}
//check for special characters in password
if(!Character.isDigit(s) && !Character.isLetter(s))
{
special=true;
}
}
//check password length
if (password.length() > 8) 
{  
length=true;
} 
//check password start with ? or !
else if (password.startsWith("?"))
{
start1=true;
}
else if (password.startsWith("!"))
{
start1=true;
}
//requirements of password for boolean true
if(letter && digit && special && length)
{
valid = true;
}
//return boolean false if detect start with ? or !, or spaces in password
if(start1||start2||space)
{
valid = false;
}

return valid;

}

/**********************************************************************************/

//this method checks that both user entered passwords match       
public static boolean passismatch(String password1, String password2){
//declare variables
boolean match;
//initialize variables
match=false;

//compare password strings   
if (password1.equals(password2))
{
match= true;
}

return match;

}

}

So I've been able to figure all except for making sure there are no consecutive repeating characters. I really don't even know where to begin with this. I've searched for hours on a few forums to try and find a solution and put it all together but without seeing an example or just something I can't figure it out for the life of me. Any help would be IMMENSELY appreciated.

  • Hmm this sounds like a recursive function – Scary Wombat Mar 10 '15 at 04:41
  • 1
    boolean noConsecutive = true; loop i from [0...n-2], compare password[i] with password[i+1], if they are same, noConsecutive = false; – Shashank Mar 10 '15 at 04:42
  • I don't fully understand how to make that happen, could you explain that a little bit more in depth? I'm really bad with loops :-\. I.E. where would I tell the loop to go to negative 2? and what would i use to compare the char? – Peter Mavrakis Mar 10 '15 at 04:52
  • you can have a look on this question asked on SO.http://stackoverflow.com/questions/19605150/regex-for-password-must-be-contain-at-least-8-characters-least-1-number-and-bot – nikhil Mar 10 '15 at 05:01
  • yea i had seen that one. they don't have the requirement of no consecutive identical characters – Peter Mavrakis Mar 10 '15 at 05:05
  • In that case you can refer this http://stackoverflow.com/questions/4918809/regex-to-check-3-or-more-consecutive-occurances-of-a-character. As using Native functionality is always the wise choice rather than writing your own custom code. – nikhil Mar 10 '15 at 05:09
  • You can also check this source using has map http://java2novice.com/java-interview-programs/duplicate-string-character-count/ – mussdroid Mar 10 '15 at 05:39

2 Answers2

2

You can get password to string array and compare each string with the next two string whether it has the same.

public static boolean hasConsecutiveCharacters(String pwd){
        String[] letter = pwd.split(""); // here you get each letter in to a string array

        for(int i=0; i<letter.length-2; i++){
            if(letter[i].equals(letter[i+1]) && letter[i+1].equals(letter[i+2])){
                return true; //return true as it has 3 consecutive same character
            }
        }
        return false; //If you reach here that means there are no 3 consecutive characters therefore return false.
    }
Isuru Gunawardana
  • 2,847
  • 6
  • 28
  • 60
  • 1
    ahh thank you . so use .split to split the array, put each character into a string, and letter.length-2 is how i get it to go back 2 spaces and check the char for same char. awesome. I really need to brush up on loops and arrays I had a hard time with arrays. Thanks man. updating above code so people can use as reference – Peter Mavrakis Mar 10 '15 at 05:17
1
package filehandling;

import java.util.Scanner;

public class PasswordValidation {

    public static void main(String[] args) {

        String enterPassword ;
        String reEnterPassword ;

        boolean passValid ;
        boolean isPassMatch ;

        Scanner password = new Scanner(System.in);
        System.out.println("Enter Your Password:");
        System.out.println("(Must be at least 8 characters,  contain a number, letter, and special character");
        enterPassword = password.nextLine();

        passValid = checkPassIsValid(enterPassword);

        if(passValid) {
            System.out.println("Re-Enter Your Password: ");
            reEnterPassword = password.nextLine();
            isPassMatch = checkPasswordMatching(enterPassword,reEnterPassword);

            if(!isPassMatch)
                System.out.println("Password do Not Match: ");
            else if (isPassMatch)
                System.out.println("Password Set.");

        }
        else
            System.out.println("Password is Not Valid.");

    }

    private static boolean checkPassIsValid(String enterPassword) {

        boolean length = false;
        boolean letter = false;
        boolean digit = false;
        boolean space = false;
        boolean startSpace = false;
        boolean special = false;
        boolean start1 = false;
        boolean start2 = false;
        boolean valid = false;

        for(int i = 0; i < enterPassword.length(); i++) {
            char c = enterPassword.charAt(i);

            if(enterPassword.length() >= 8)
                length = true;
            if(Character.isDigit(c))
                digit = true;
            if(Character.isLetter(c))
                letter = true;
            if(Character.isSpaceChar(c))
                space = true;
            if( (!Character.isDigit(c)) && (!Character.isLetter(c)) )
                special = true;
            if(enterPassword.startsWith(" "))
                startSpace = true;
            else if(enterPassword.startsWith("?"))
                start1 = true;
            else if(enterPassword.startsWith("!"))
                start2 = true;
            if(letter && digit && special && length)
                valid = true;
            if(start1 || start2 || startSpace)
                valid = false;
        }
        return valid;

    }

    private static boolean checkPasswordMatching(String enterPassword,String reEnterPassword) {

        if(enterPassword.equals(reEnterPassword))
            return true;        
        return false;
    }

}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
VIKAS
  • 15
  • 4