简体   繁体   中英

Debugging password verification code in java

I've been trying to create a password verification using java. The requirements for the password verification should be:

1) The password should be at least six characters long.

2) The password should at least contain one uppercase and lowercase.

3) The password should have at least one digit.

I have done both 1 and 2, but I can't figure out number 3.

I have made a boolean method for checking characters if it has digits. Supposedly, it'll return true if it was able to find a digit, but even if I have entered a password with a digit, it still returns false.

I wonder what's wrong with my code. Hope you guys can help me with this.

package PasswordVerifier;
import java.util.Scanner;
public class PasswordVerifierMain {

public static void main(String[]args){
    Scanner hold = new Scanner(System.in);
    String pass;
    System.out.print("Enter password:");
    pass = hold.nextLine();

    if(isValid(pass) && pass.length() >= 6){

        boolean hasUppercase = pass.equals(pass.toUpperCase());
        boolean hasLowercase = pass.equals(pass.toLowerCase());
        if(!hasUppercase){
            System.out.print("Must have atleast one uppercase letter!");
        }else if(!hasLowercase){
            System.out.print("Must have atleast one lowercase letter!");
        }else{
            System.out.print("Password Successful!");
        }

    }else{
        System.out.print("Invalid Password! Minimum six characters!");
    }

    System.out.println();
    System.out.println(isValid(pass));
}

private static boolean isValid(String pass){
    boolean status = true;
    int i = 0;
    while(status && i < pass.length()){
        if(!Character.isDigit(pass.charAt(i))){
            status = false;
        }
        i++;
    }

    return status;
}

}

You set status in each iteration, effectively overwriting any true value except for the last character. Besides that you should assume status = false and set it to true if you found a digit. Your code considers any password that contains other characters as well as invalid.

Example:

pass = "1abcde"
status = true

1st iteration: char = 1 -> a digit, so status is not changed 
2nd iteration: char = a -> not a digit, so status will be set to false

loop will break since status is false -> result: not valid

To solve this first assume status = false and when you find a digit set status = true . That way when the password contains a digit it will be considered valid.

Example:

status = false; //assume invalid password
for( char c : pass.toCharArray() ) {
  if( Character.isDigit( c ) ) {
    //found a digit
    status = true;

    //we're done since we found at least one digit
    break;
  }
}
return status;

Your approach could be improved, though:

Iterate over the characters and collect their traits (eg upper case, lower case, digit, special character etc.). Then check the number of traits and whether required traits are present.

Example:

enum CharTraits {
  LOWER_CASE,
  UPPER_CASE,
  DIGIT,
  SPECIAL
}

Set<CharTraits> getPasswordTraits( String pw ) {
  Set<CharTraits> traits = new HashSet<>();

  for( char c : pw.toCharArray() ) {
    //better check for index of 0-9 otherwise you allow non-ascii digits as well
    if( Character.isDigit( c ) ) {
      traits.add( DIGIT );
    }
    else if ( Character.isUpperCase( c ) {
      traits.add( UPPER_CASE);
    }
    ... //the rest
  }

  return traits;
}

boolean isValid(String pass){
  Set<CharTraits> traits = getPasswordTraits( String pass )

  //Check for digits (just an example, you could check for more) 
  return traits.contains( DIGIT );

  //if you want to check for 3 out of 4 just do this:
  //return traits.size() >= 3;
}

If your password is abc123 the method sees the first character, sees it is no digit so it sets "status" to false. Thats why your while would not go into the second round. status has to be true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM