user14357849
user14357849

Reputation:

Check password, verify, and loop again

I have a problem that requires at least 2 uppercase letters, at least 3 lowercase letters and 1 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 three lowercase letters, and at least one digit. Continuously prompt the user until a valid password is entered. Display Valid password if the password is valid; if not, display the appropriate reason(s) the password is not valid as follows:

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 "passWOrd12", your program should output: valid password

Here is my coding so far, I am having the problem of the program promting the user to enter in more passwords once they enter and incorrect one.

import java.util.*;

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 = "digits";
        }
        if (capCount < 2) {
            result = "uppercase letters";
        }
        if (numCount < 2 && capCount < 2) {
            result = "uppercase letters digits";
        }

        if (length < 2) {
            result = "Password is Too Short!";
        }
        return (result);
    }
}

Upvotes: 0

Views: 472

Answers (3)

user14357849
user14357849

Reputation:

Below is the finished code that worked on the Cengage platform for me:

import java.util.*;
public class ValidatePassword {
public static void main(String[] args) {
    String inputPassword;
    Scanner input = new Scanner(System.in);
    boolean success=false;
    while(!success){

    System.out.print("Password: ");
    inputPassword = input.next();
    System.out.println(PassCheck(inputPassword));
    if(PassCheck(inputPassword).equals("Valid Password")) success = true; 
    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 = "digits";
    }
    if (capCount < 2) {
      result = "uppercase letters";
    }
    if (capCount < 2) {
      result = "uppercase letters";
    }
    if (numCount < 2 && capCount < 2) 
    {
      result = "uppercase letters digits";
    }

    if (length < 2) {
      result = "Password is Too Short!";
    }
    return (result);
  }
}

Upvotes: 0

Mustafa Poya
Mustafa Poya

Reputation: 3027

you can do it by changing the return type of PassCheck method to a boolean result and use Character class to check for Uppercase Lowercase Digit characters and count occurence of each one at the end check for conditions and if it pass then the result is true else it is false and you can iterator your do-while according to result of PassCheck method

public class ValidatePassword {
    public static void main(String[] args) {
        String inputPassword;
        Scanner input = new Scanner(System.in);
        boolean isValidPassword = false;
        
        do {
            System.out.print("Password: ");
            inputPassword = input.next();
            isValidPassword = PassCheck(inputPassword);
            System.out.println("");
        }while(!isValidPassword);
    }
    
    public static boolean PassCheck(String Password) {
        boolean isValid = false;
        String result = "";
        int numCount = 0;
        int capCount = 0;
        int lowCount = 0;
        
        for (int x = 0; x < Password.length(); x++) {
            
            if(Character.isDigit(Password.charAt(x))) {
                numCount++;
            }
            if(Character.isUpperCase(Password.charAt(x))) {
                capCount++;
            }
            
            if(Character.isLowerCase(Password.charAt(x))) {
                lowCount++;
            }
        }
        
        if(numCount >= 1 && capCount >= 2 && lowCount >= 3) {
            result = "Password Valid";
            isValid = true;
        } else {
            isValid = false;
            result = "Password is Invalid: ";
            
            if(Password.length() < 2) {
                result += " Password is Too Short!";
            }
            
            if(numCount < 1) {
                result += " no digit";
            }
            
            if(capCount < 2) {
                result += " at lease 2 Uppercase";
            }
            
            if(lowCount < 3) {
                result += " at lease 3 Lowercase";
            }
        }
        System.out.println(result);
        return isValid;
    }
}

Upvotes: 0

Smartsav10
Smartsav10

Reputation: 123

You need a while loop. This will ensure that your code will keep looping until it succeeds. When it succeeds, the boolean becomes true and it doesn't loop. Write whatever you want to happen next after the while loop.

public static void main(String[] args) {
    String inputPassword;
    Scanner input = new Scanner(System.in);
    boolean success=false;
    while(!success){

    System.out.print("Password: ");
    inputPassword = input.next();
    System.out.println(PassCheck(inputPassword));
    if(PassCheck(inputPassword).equals("Valid Password")) success = true; 
    System.out.println("");

    } 
  }

Upvotes: 1

Related Questions