Java code Check Password for Letter and Digit?

2019-07-31 03:38发布

问题:

EDIT--VERSION The first post was confusamagin. My assignment is to create a password prompt program. The password needs to be checked to see if it does have at least one digit and one letter in it. Also the password length must be between 6 - 10.

My problem is trying to figure out how see if a digit and letter exist the password. In the check password area I am not sure where to begin really. I am not sure how to see if it has a Letter and a Digit in one. I know how to do either or by using a for statement to count and check but all it does is check to see rather it contains all letters or all digits.

Below is what I have so far...

import java.util.Scanner;

class Password {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

//------ENTER A USERNAME
        System.out.println("Welcome please enter your username and password.");
        System.out.print("Username >>");
        input.nextLine();


//------PASSWORD AUTHENTICATION BEGIN       
        String password = enterPassword();
            while ( !checkPassword(password) ) {
            System.out.println("Password must be 6 - 10 characters long!"); 
            password = enterPassword();
            }

//------PASSWORD VERIFY
        String passwordverify = enterPassword();
        while (!password.equals(passwordverify)){
            System.out.println("ERROR - Passwords DO NOT MATCH Re-Enter Passwords Again");
            password = enterPassword();

        }

//------ACCEPT PASSWORD     
        System.out.println("Username and Password Accepted!");

        }


//--ENTER PASSWORD STATEMENT
    public static String enterPassword(){
        String password;
        Scanner input = new Scanner(System.in);
        System.out.print("Password >>");
        password = input.nextLine();
        return password;
        }

//--BOOLEAN CHECK PW    
    public static boolean checkPassword(String password){
        int length;
        length = password.length();
            if (length < 6 || length > 11){
            return false;
            }
                    for (int i = 0; i < password.length();i++){
        if (!Character.isLetter(password.charAt(i)))
        return false;
            }           
            return true;
        }

}

回答1:

  public static boolean checkPasswordLetter(String password){
            for (int i = 0; i < password.length();){
            if (!Character.isLetter(password.charAt(i))){
                return false;
                }
            }
        return true;
        }

Here you didn't increment variable i , need in for i++ or your loop is going forever if is not letter, same and in checkPasswordDigit



回答2:

checkPasswordLetter and checkPasswordDigit will only return true if ALL chars are letters/digits respectively. Is this what you meant?



回答3:

First off... It's not that Java is not looping or checking Boolean. Java is doing what you are telling it to do.

Now, what you want to do is different than what you are doing.

What you need to do is something like:

public static void main(String[] args) {
    // ...
    String password = enterPassword();
        while ( !isPasswordValid(password) ) {            
        password = enterPassword();
    }
    System.out.println("Username and Password Accepted!");
}

public static boolean isPasswordValid(String password) {
    // return true if and only if password:
    // 1. has 6-10 characters
    // 2. contains at least one digit
    // 3. contains at least one character
    // print messages accordingly
}


回答4:

There are two things wrong.

  1. Your letter checking is failing on the first non-letter. Same thing happening with your digit checking. You only want to reject if every character is a non-letter, for example. So logic error.

  2. You have three loops. Bad idea, because if you pass the length check once, it is never going to be checked again. Consider what would happen if someone entered: 12345678. Length ok, but no letter. Ok, now enter: a1. Length not checked again.



回答5:

import java.util.Scanner;

import java.util.*;
import java.lang.String;
import java.lang.Character;

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) {
        if (password.length() < 9) { 
            return false;
        }

else {
        char c = 0 ;  
              int  count=0;
              System.out.println(password.length());
        for (int i = 0;i<=password.length()-1; i++) 
                {
c = password.charAt(i);
System.out.println(c);
if (!Character.isLetterOrDigit(c))
{       
return false;        
}
else if (Character.isDigit(c))
{   
count++;    
if(count==3)
{
return false;
}
}
}
                return true;
}
                }

                }


回答6:

import java.util.Scanner;
public class password{
public static void main(String[] args) {
  // Create a Scanner
  Scanner input = new Scanner(System.in);

     // Prompt the user to enter a string
 System.out.print("Enter a password ") ;
  String s = input.nextLine();


 if(isValid(s))
System.out.println(s + " is a valid password");
else
System.out.println(s + " is not a valid password");
}

public static boolean isValid(String s ) 
{

  int i = 0,j=0;


 if(s.length()>=8)
     {
    for(i=0;i<s.length();i++)
   {
     //if(Charcter.isLetter(s.charAt(i))||Digit.isLetter(s.charAt(i)))
   if (Character.isLetterOrDigit(s.charAt(i)))
 {
    if(Character.isDigit(s.charAt(i)))
    j++;
 }
  }
    }
  else
   return false;
    if(j>=2)   
     return true;
       return false;
     } }