特殊字符检查,if 语句不起作用。 Java

Special character check, if statement not working. Java

首先,我是 java 的新手,所以我不确定是否有更简单的方法来做到这一点。我目前正在创建密码检查器。除了我的特殊字符检查

之外,我的大部分代码 运行 都很顺利

我的代码检查长度和是否有数字,但它不适用于我的特殊字符检查。我目前将 if 语句设置为如果字符串中的一个字符不是字母、数字或 space,则 specialCharCheck = true。否则,保持为假。

import java.util.Scanner;

public class Project5_Part1 {

    public static void main(String[] args) {
        String pass;
        String confirm;
        boolean parameters;
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Please enter password : ");
        pass = in.nextLine();
        
        System.out.print("Please re-enter the password to confirm : ");
        confirm = in.nextLine();
        
        parameters = isValid(pass);
        
        while (!pass.equals(confirm) || (!parameters)) 
        {
            System.out.println("The password is invalid");
            
            System.out.print("Please enter the password again : ");
            pass = in.nextLine();
            
            System.out.print("Please re-enter the password to confirm : ");
            confirm = in.nextLine();
            
            parameters = isValid(pass);

        }
        if (isValid(pass)) 
        {
            System.out.println("The password is valid");
        }
    }

    public static boolean isValid(String pass) {
        boolean numberCheck = false;
        boolean specialCharCheck = false;

        if (pass.length() < 8) {
            return false;
        } else {

            for (int i = 0; i < pass.length(); i++) 
            {
                if(Character.isDigit(pass.charAt(i))) 
                {
                    numberCheck = true;
                }
                
                if (!Character.isLetterOrDigit(i) && !Character.isSpaceChar(i))
                {
                    specialCharCheck = true;
                    //System.out.println("Test");
                }
                else 
                {
                    specialCharCheck = false;
                }

                
            }
            return (numberCheck && specialCharCheck);

        }
    }
}

我错过了一个简单的错误吗?我知道还有另一种方法可以做到这一点,但我觉得这样做看起来更有效率。这个逻辑对我来说很有意义。

Character.isLetterOrDigit(char) 需要一个字符作为参数,但您却传递了一个 'int i'。你需要有,

if (!Character.isLetterOrDigit(pass.charAt(i)) && !Character.isSpaceChar(pass.charAt(i)))
                {
                    specialCharCheck = true;
                    //System.out.println("Test");
                }
                else 
                {
                    specialCharCheck = false;
                }

这里有一个解决方案 while:

    public static boolean isValid(String pass) {
        boolean numberCheck = false;
        boolean specialCharCheck = false;

        if (pass.length() < 8) {
            return false;
        } else {
            int i = 0;
            while (i < pass.length() && (!numberCheck || !specialCharCheck)) {
                if (!numberCheck && Character.isDigit(pass.charAt(i))) {
                    numberCheck = true;
                }
                if (!specialCharCheck && !Character.isLetterOrDigit(i) && !Character.isSpaceChar(i)) {
                    specialCharCheck = true;
                }
                i++;
            }
            return (numberCheck && specialCharCheck);

        }
    }