简体   繁体   中英

Creating a password with only numbers and digits using for loops

I am making a program that takes a string from a user to create a password. However, this password needs to be at least 8 characters or more, and it can only include letters(uppercase and lowercase) and digits. I already did this, however, when I enter in the user input a blank space(ex: "pass word") or a special symbol such as "%" or "&", the method still returns the value true, making the password valid when it shouldn't return this, how do I correct this?

import java.util.Scanner;
public class Password
{
    public static void main(String[] args)
    {
        System.out.println("Enter your password");
        Scanner input = new Scanner(System.in);
        String pass = input.nextLine();
        System.out.println("Is the password valid? " + passwordCheck(pass));
    }
    
    public static boolean passwordCheck(String password)
    {   
        boolean pass = false;
        for(int i=0; i < password.length(); i++)
        {
        char c = password.charAt(i);
        if(password.length() >= 8)
        {
            if(c >= 48 && c <= 57)
            {
            pass = true;    
            }
            else if(c>= 97 && c<= 122)
            {
            pass = true; 
            }
            else if(c>=65 && c<=90)
            {
            pass = true;    
            }
            else if(c == 32)
            {
            pass = false;    
            }
        }    
        
        
        }
        return pass;

    }
}

you need to break out of the loop once it encounters a space or any other special character.

import java.util.Scanner;
public class Password
{
    public static void main(String[] args)
    {
        System.out.println("Enter your password");
        Scanner input = new Scanner(System.in);
        String pass = input.nextLine();
        System.out.println("Is the password valid? " + passwordCheck(pass));
    }
    
    public static boolean passwordCheck(String password)
    {   
        boolean pass = false;
        for(int i=0; i < password.length(); i++)
        {
        char c = password.charAt(i);
        if(password.length() >= 8)
        {
            // also you can directly mention the character instead of looking for its respective ASCII value
            if(c >= '0' && c <= '9')
            {
                pass = true;
            }
            else if(c>= 'a' && c<= 'z')
            {
                pass = true; 
            }
            else if(c>='A' && c<='Z')
            {
                pass = true;
            }
            else // loop will break if it encounters a space or any other 
 special character
            {
                pass = false;    
                break;
            }
        }    
        
        
        }
        return pass;

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM