Search code examples
javapasswordsverification

Password Checking, Verification and Requirements


I have a problem that requires at least 2 uppercase letters, at least 2 lowercase letters and 2 digits.

Here is the exact problem:

Write an application that prompts the user for a password that contains at least two uppercase letters, at least two lowercase letters, and at least two digits. After a password is entered, display a message indicating whether the user was successful or the reason the user was not successful.

For example, if the user enters "Password" your program should output: Your password was invalid for the following reasons: uppercase letters digits

If a user enters "P4SSw0rd", your program should output: valid password

Here is my coding so far, I am having the problem of including output lines. For example, if someone doesn't have 2 capital letters AND doesn't have 2 letters. When writing 1 letter, it doesn't include both failures in the output.

import java.util.Scanner;
public class ValidatePassword {
  public static void main(String[] args) {
    String inputPassword;
    Scanner input = new Scanner(System.in);
    System.out.print("Password: ");
    inputPassword = input.next();
    System.out.println(PassCheck(inputPassword));
    System.out.println("");
  }

  public static String PassCheck(String Password) {
    String result = "Valid Password";
    int length = 0;
    int numCount = 0;
    int capCount = 0;
    for (int x = 0; x < Password.length(); x++) {
      if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91) ||
        (Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
      } else {
        result = "Password Contains Invalid Character!";
      }
      if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) {
        numCount++;
      }
      if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) {
        capCount++;
      }
      length = (x + 1);
    }
    if (numCount < 2) {
      result = "Not Enough Numbers in Password!";
    }
    if (capCount < 2) {
      result = "Not Enough Capital Letters in Password!";
    }
    if (length < 2) {
      result = "Password is Too Short!";
    }
    return (result);
  }
}

Solution

  • If I understand correctly what you want to do is when you enter e.g "Password" you don't have 2 capital letters and 2 digits so your output should looks like: "Not Enough Numbers in Password! Not Enough Capital Letters in Password!". I suggest 2 solutions:

    1. If you want to add one String to another use + because you overwrite first result value with another. But this is not best solution because every time you add value to String you create new String on String pool. More information here: https://stackoverflow.com/a/1553110/6003541

      result += "Password is Too Short!";
      

      or

      result = result + "Password is Too Short!";
      
    2. I suggest to use StringBuilder. Use method "append" to add your result and at the end return toString() value of StringBuilder object.

      StringBuilder sb = new StringBuilder(); 
      if (numCount < 2) {
        sb.append("Not Enough Numbers in Password!");
        sb.append(System.getProperty("line.separator"));
      }
      if (capCount < 2) {
        sb.append("Not Enough Capital Letters in Password!");
        sb.append(System.getProperty("line.separator"));
      }
      if (length < 2) {
        sb.append("Password is Too Short!");
      }
      
      return sb.toString();