Checking Password Code

三世轮回 提交于 2019-12-04 19:24:04

You almost got it. There are some errors though:

  • you're not iterating over all the chars of the password (i < password.length() - 1 is wrong)
  • you start with a digit count of 1 instead of 0
  • you make the check that the count of digits is at least 2 as soon as you meet the first digit, instead of checking it after you have scanned all the characters
Abdul Hanan Khan

Suppose a valid password has:

  • 8 or more characters, but not more than 16 characters
  • one or more uppercase characters
  • one or more lowercase characters
  • one or more digits
  • one or more special characters (like $, @, or !)

Code:

import java.util.Scanner;
public class Password {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int min =8;
    int max=16;
    int digit=0;
    int special=0;
    int upCount=0;
    int loCount=0;
    String password;
    Scanner scan = new Scanner(System.in);
    System.out.println(" Enter Your Password:");
        password = scan.nextLine();
    if(password.length()>=min&&password.length()<=max){
        for(int i =0;i<password.length();i++){
            char c = password.charAt(i);
            if(Character.isUpperCase(c)){
                upCount++;
            }
            if(Character.isLowerCase(c)){
                loCount++;
            }
            if(Character.isDigit(c)){
                digit++;
            }
            if(c>=33&&c<=46||c==64){
                special++;
            }
        }
        if(special>=1&&loCount>=1&&upCount>=1&&digit>=1){
            System.out.println(" Password is good:");
        }

    }

    if(password.length()<min){

        for(int i =0;i<password.length();i++){
            char c = password.charAt(i);
            if(Character.isLowerCase(c)){
                loCount++;
            }
            }

        if(loCount>0){
            System.out.println(" Password must be atleat "+min+" characters:");
            System.out.println(" You need atleast one upper case chracter:");
            System.out.println(" You need atleast one digit:");
            System.out.println(" You need atleast one special chracter:");



    }
    }
    else if(password.length()<min&&upCount>1){
        for(int i =0;i<password.length();i++){
        char c =password.charAt(i);
        if(Character.isLowerCase(c)){
            loCount++;
        }
         if(Character.isUpperCase(c)){
            upCount++;
        }
        }
        if(loCount>0&&upCount>0){
        System.out.println(" Password must be atleast "+min+" chracters:");
        System.out.println(" You need atleast one digit:");
        System.out.println(" You need atleast one special chracter:");
    }
    }
     if(password.length()>max||password.length()>=max&&upCount>1&&loCount>1&&digit>1){
         System.out.println(" Password is too long.Limit is "+max+" chracters:");
                 System.out.println(" You need atleast one special chracter:");

        }
      if(password.length()>=min&&password.length()<=max&&loCount>0&&upCount>0&&digit>0&&special==0){
         System.out.println(" You need atleast a special chracter");
     }
      if(password.length()>=min&&password.length()<=max&&loCount>0&&upCount>0&&digit==0&&special==0){
         System.out.println(" You need atleast one digit:");
         System.out.println(" You need atleast one special chracter:");
     }
   }
}

As previously answered, you should chek all the password characters first. Count your digits and finally check if count is smaller than 2. Here is the referring code.

if (password.length() < 8) {   
        return false;  
    } else {      
        char c;  
        int count = 0;   
        for (int i = 0; i < password.length(); i++) {  
            c = password.charAt(i);  
            if (!Character.isLetterOrDigit(c)) {          
                return false;  
            } else if (Character.isDigit(c)) {  
                count++;     
            }  
        }  
        if (count < 2)   {     
            return false;  
        }  
    }  
    return true;  
}  
public void run()
{
    String password= readLine("Insert Password: ");
    boolean len= true;
    boolean letter= true;
    boolean twodig= true;
       if (password.length() < 8) {   
         len = false;  
    } else {      
        char c;  
        int count = 0;   
        for (int i = 0; i < password.length(); i++) {  
            c = password.charAt(i);  
            if (!Character.isLetterOrDigit(c)) {          
                letter = false;  
            } else if (Character.isDigit(c)) {  
                count++;     
            }  
        }  
        if (count < 2)   {     
            twodig = false;  
        }  
    }  
    if(len ==true && letter == true && twodig == true)
    {
        System.out.println("This password is valid ");
    }
    else
    {
        System.out.println("This password is invalid");
    }
}

package Method;

/* 2. Write a Java method to check whether a string is a valid password. Password rules: A password must have at least ten characters. A password consists of only letters and digits. A password must contain at least two digits.

Expected Output:

  1. A password must have at least eight characters.
  2. A password consists of only letters and digits.
  3. A password must contain at least two digits
    Input a password (You are agreeing to the above Terms and Conditions.): abcd1234
    Password is valid: abcd1234 */

public class CheckPassword {

public static String password;
public static int disitCounter = 0;

public static boolean isValid(String password) {


    if (password.length() >= 10 ) {
        for(int index = 0; index < password.length(); index++) {
            char passChar = password.charAt(index);
            if (!Character.isLetterOrDigit(passChar)) {
                return false;
            }
            else {
                if (Character.isDigit(passChar)) {
                    disitCounter++;
                }
            }

        }

    }
    if(disitCounter < 2) {
        return false;
    }
    return true;
}

public static void main(String[] args) {
    password = "abcdefgh1w3";
    if(isValid(password)) {
        System.out.print("It is a valid password");
    }
    else {
        System.out.print("It is a invalid password");
    }
}

}

Parag Satav
package com.parag;

/*
 * @author Parag Satav
 */
public boolean check(String password) {

    boolean flagUppercase = false;
    boolean flagLowercase = false;
    boolean flagDigit = false;
    boolean flag = false;

    if (password.length() >= 10) {

        for (int i = 0; i < password.length(); i++) {

            if (!Character.isLetterOrDigit(password.charAt(i))) {
                return false;
            }

            if (Character.isDigit(password.charAt(i)) && !flagDigit) {
                flagDigit = true;

            }

            if (Character.isUpperCase(password.charAt(i)) && !flagUppercase) {
                flagUppercase = true;

            }

            if (Character.isLowerCase(password.charAt(i)) && !flagLowercase) {
                flagLowercase = true;
            }
        }
    }

    if (flagDigit && flagUppercase && flagLowercase) {
        flag = true;
        System.out.println("Success..");
    } else
        System.out.println("Fail..");

    return flag;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!