制作密码验证器的规则之一是:密钥必须包含“#”或“_”,但不能同时包含两者

Making A Password Validator And One Of Rules Is: The Key Must Contain Either A '#' or A '_', But NOT Both

我正在制作一个 Password/Key 验证器,其中包含一组必须满足的规则才能验证 Password/Key。

这些规则如下: - 密钥至少 7 个字符长,最多 20 个字符长,并且 - 密钥不得以特殊字符“#”或“”开头,并且 - 密钥在任何地方都不能有 space 字符,并且 - 密钥必须至少有一个大写字符和至少一个小写字符,并且 - 密钥不得包含用户名,并且 - 密钥必须包含“#”或“”,但不能同时包含两者。

除最后一条规则外,我已设法使所有内容正常工作"The key must contain either a '#' or a '_', but not both."

我目前的代码如下。我刚开始学习java所以请理解。

 * Asks user for key word and the name and then checks if it is a valid key word.
 */
public void doCompletion(){
    String key = UI.askString("Key:   ");
    String name = UI.askString("Your name:   ");
    this.validateKeyCompletion(key, name);
}

/** COMPLETION
 * Report that the key is valid or report ALL the rules that the key failed.
 */
public void validateKeyCompletion(String key, String name){
    /*# YOUR CODE HERE */
   int characterNumber = key.length();
   boolean hasUppercase;
   boolean hasLowercase;
   hasUppercase = !key.equals(key.toLowerCase());
   hasLowercase = !key.equals(key.toUpperCase());
   String specialChars = "(.*[ #  _  ].*)";
   if (characterNumber < 7 || characterNumber > 20){
    UI.println("Invalid: Key length must not be less than 7 or greater than 20");
   }
   else if (key.startsWith ("#") || (key.startsWith ("_"))){
    UI.println("Invalid: Key cannot start with '#' or '_'");
   } 
   else if (key.contains(" ")){
    UI.println("Invalid: Key cannot contain ' '");
   }
   else if(!hasUppercase)
   {
    UI.println("Invalid: Key must contain an uppercase character");
   }
   else if(!hasLowercase)
   {
    UI.println("Invalid: Key must contain a lowercase character");
   }
   else if(key.matches(name)){
    UI.println("Invalid: Key cannot contain Username");
   }
   else if(!key.matches(specialChars)){
    UI.println("Invalid: Key must contain either a '#' or a '_', but not both");
   } 
   else {
    UI.println("Valid");
   }


}

试试下面的方法并使用 JAVA 中的 indexOf 方法:

 * Asks user for key word and the name and then checks if it is a valid key word.
 */
public void doCompletion(){
    String key = UI.askString("Key:   ");
    String name = UI.askString("Your name:   ");
    this.validateKeyCompletion(key, name);
}

/** COMPLETION
 * Report that the key is valid or report ALL the rules that the key failed.
 */
public void validateKeyCompletion(String key, String name){
    /*# YOUR CODE HERE */
   int characterNumber = key.length();
   boolean hasUppercase;
   boolean hasLowercase;
   hasUppercase = !key.equals(key.toLowerCase());
   hasLowercase = !key.equals(key.toUpperCase());
   String specialChars = "(.*[ #  _  ].*)";
   if (characterNumber < 7 || characterNumber > 20){
    UI.println("Invalid: Key length must not be less than 7 or greater than 20");
   }
   else if (key.startsWith ("#") || (key.startsWith ("_"))){
    UI.println("Invalid: Key cannot start with '#' or '_'");
   } 
   else if (key.contains(" ")){
    UI.println("Invalid: Key cannot contain ' '");
   }
   else if(!hasUppercase)
   {
    UI.println("Invalid: Key must contain an uppercase character");
   }
   else if(!hasLowercase)
   {
    UI.println("Invalid: Key must contain a lowercase character");
   }
   else if(key.matches(name)){
    UI.println("Invalid: Key cannot contain Username");
   }
   else if(key.indexOf('#') > -1 && key.indexOf('_') > -1){
    UI.println("Invalid: Key must contain either a '#' or a '_', but not both");
   } 
   else {
    UI.println("Valid");
   }


}

以下正则表达式检查密钥不是以 '#''_' 开头,而是包含其中一个字符,但不能同时包含这两个字符。

    if (!key.matches("^[^#_]+[#_]{1,1}[^#_]*")) {
        UI.println("Invalid: Key must contain either a '#' or a '_', but not both");
    }