controleren Password Code

Probleem Beschrijving:

Sommige websites opleggen bepaalde regels voor wachtwoorden. Schrijf een methode die controleert of een string is een geldig wachtwoord. Stel dat de wachtwoord regel is als volgt:

  • Een wachtwoord moet ten minste acht tekens lang zijn.
  • Een wachtwoord bestaat uit alleen letters en cijfers.
  • Een wachtwoord moet minstens twee cijfers bevatten.

Schrijf een programma dat de gebruiker vraagt ​​om een ​​wachtwoord en geeft “geldig wachtwoord” in te voeren als de regel wordt gevolgd of “ongeldig wachtwoord” op een andere manier.

Dit is wat ik heb tot nu toe:

import java.util.*;  
import java.lang.String;  
import java.lang.Character;  
/**
 * @author CD
 * 12/2/2012
 * This class will check your password to make sure it fits the minimum set     requirements.
 */
public class CheckingPassword {  
    public static void main(String[] args) {  
        Scanner input = new Scanner(System.in);  
        System.out.print("Please enter a Password: ");  
        String password = input.next();  
        if (isValid(password)) {  
            System.out.println("Valid Password");  
        } else {  
            System.out.println("Invalid Password");  
        }  
    }  
    public static boolean isValid(String password) {  
        //return true if and only if password:
        //1. have at least eight characters.
        //2. consists of only letters and digits.
        //3. must contain at least two digits.
        if (password.length() < 8) {   
            return false;  
        } else {      
            char c;  
            int count = 1;   
            for (int i = 0; i < password.length() - 1; i++) {  
                c = password.charAt(i);  
                if (!Character.isLetterOrDigit(c)) {          
                    return false;  
                } else if (Character.isDigit(c)) {  
                    count++;  
                    if (count < 2)   {     
                        return false;  
                    }     
                }  
            }  
        }  
        return true;  
    }  
}

Wanneer ik het programma uitvoer, controleert het alleen voor de lengte van het wachtwoord, ik kan er niet achter komen hoe u ervoor moet zorgen dat deze zowel letters als cijfers inchecken en ten minste twee cijfers in het wachtwoord hebben.


Antwoord 1, Autoriteit 100%

Je hebt het bijna. Er zijn echter enkele fouten:

  • U bent niet van alle tekens van het wachtwoord (i < password.length() - 1is verkeerd)
  • U begint met een cijfer van 1 in plaats van 0
  • U maakt de controle of de telling van cijfers minimaal 2 is zodra u aan het eerste cijfer voldoet, in plaats van het te controleren nadat u alle personages hebt gescand

Antwoord 2, Autoriteit 200%

Stel dat een geldig wachtwoord heeft:

  • 8 of meer tekens, maar niet meer dan 16 tekens
  • een of meer hoofdletters
  • een of meer kleine letters
  • een of meer cijfers
  • een of meer speciale tekens (zoals $, @, of!)

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:");
     }
   }
}

Antwoord 3

Zoals eerder beantwoord, moet u eerst alle wachtwoordtekens controleren. Tel je cijfers en controleer ten slotte of het aantal kleiner is dan 2.
Hier is de verwijzende 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;  
}  

Antwoord 4

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");
    }
}

Antwoord 5

pakketmethode;

/*
2. Schrijf een Java-methode om te controleren of een string een geldige is
wachtwoord.
Wachtwoord regels:
Een wachtwoord moet minimaal tien tekens bevatten.
Een wachtwoord bestaat alleen uit letters en cijfers.
Een wachtwoord moet minimaal twee cijfers bevatten.

Verwachte uitvoer:

  1. Een wachtwoord moet minimaal acht tekens bevatten.
  2. Een wachtwoord bestaat alleen uit letters en cijfers.
  3. Een wachtwoord moet minimaal twee cijfers bevatten
    Voer een wachtwoord in (u gaat akkoord met de bovenstaande algemene voorwaarden.): abcd1234
    Wachtwoord is geldig: abcd1234
    */

openbare klasse 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");
    }
}

}


Antwoord 6

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;
}

Other episodes