密码检查程序-检查大写,小写字母,数字和特殊字符 [英] Password Check Program - checking capitals, lowercase letters, digit and special character

查看:185
本文介绍了密码检查程序-检查大写,小写字母,数字和特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作业,要求我创建一个密码检查程序.

I have an assignment that requires me to create a password checking program.

密码必须至少包含8个字符,包含大写和小写字母,一个数字和一个特殊字符.

The password must be at least 8 characters, contain both capital and lower case letters, a digit and a special character.

我相信我已经接近解决这个问题,但是我的技能仍在发展,而且已经碰壁了.

I believe I am close to solving this problem but my skills are still developing and I've hit a wall.

package project1;

/**
 *
 * @author danechristian
 */
import java.util.*;
public class Project1 
{
    static Scanner console = new Scanner(System.in);
    static final String SPECIAL_CHARACTERS = "!,#,$,%,^,&,*,|";
    static String password;

    public static void main(String[] args) 
    {

        System.out.println("Create a password: ");
        password = console.next();

        if (validPassword(password))
        {
            System.out.println("Password Saved");
        }
        else
        {
            System.out.println("Invalid Passowrd. Password "
                    + "must contain atleast 1 capital letter"
                    + "1 lower case letter, 1 digit, 1"
                    + "special character (!#$%^&*|) and "
                    + "be atleast 8 characters long");
        }
    }

public static boolean validPassword(String password)
    {
        boolean upCase = false;
        boolean loCase = false;
        boolean isDigit = false;
        boolean spChar = false;

        if (password.length() >= 8)
           {
            for (int i = 0; i < password.length() - 1; i++)
               {
                if (Character.isUpperCase(password.charAt(i)))
                {
                    upCase = true;
                }

                if (Character.isLowerCase(password.charAt(i)))
                {
                    loCase = true;
                }

                if (Character.isDigit(password.charAt(i)))
                {
                    isDigit = true;
                }

                if (SPECIAL_CHARACTERS.contains(password))
                {
                    spChar = true;
                }
               } 
           }         
        return (upCase && loCase && isDigit && spChar);
    }   

}

推荐答案

为了检查是否有以下内容:

In order to check have something like this:

public static boolean validPassword(String password){
        boolean upCase = false;
        boolean loCase = false;
        boolean isDigit = false;
        boolean spChar = false;
     if (password.length()>7){
          if (password.matches(".+[A-Z].+")){
              upCase = true;
          }
          if (password.matches(".+[a-z].+")){
              loCase = true;
          }
          if (password.matches(".+[1-9].+")){
              isDigit = true;
          }
          if (SPECIAL_CHARACTERS.contains(password)){
               spChar = true;
          }
     }
  return (upCase && loCase && isDigit && spChar);
} 

这篇关于密码检查程序-检查大写,小写字母,数字和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆