如何验证特殊字符,例如 !在Java中不使用正则表达式的密码?

发布于 2025-01-12 02:31:50 字数 2386 浏览 4 评论 0原文

我是 Java 新手,我的第一个任务是构建一个密码验证器。我不确定是操作符的问题还是我设置字符串的方式。我似乎无法正确验证。另外,如何在不使用正则表达式的情况下验证特殊字符(!%$#...)。

导入java.util.Scanner;

公开课密码{

public static void main(String[] args) {
    
     
     final int MAX=12;
     final int MIN= 7;
     final int MIN_Uppercase=1;
     final int MIN_Lowercase=1;
     final String SPECIAL="!";
     
     int maxEl=0;
     int minEl=0;
     int upperCount=0;
     int lowerCount=0;
     String specialX = "!";
     
     
     
    // Print a message asking the user to input a password via the console 
        System.out.println("The password must contain:\r\n"
                + " At least one lowercase letter\r\n"
                + " At least one uppercase letter\r\n"
                + " At least minimum 7 characters\r\n"
                + " At least maximum 12 characters\r\n"
                + " An exclamation point: !"
                + "Please enter a password: ");
        
    //Receive the input via the console
        Scanner scan = new Scanner(System.in);
        String password = scan.nextLine();
        int passwordLen = password.length();
        
        
    //Check if there is at least one lower case letter  
    for (int i = 0; i < passwordLen; i++ ) {
        char chr = password.charAt(i);
        
        //Check if there is at least one upper case letter
        
        if (Character.isUpperCase(chr))
            upperCount++;
        
        //Check if there is at least one lower case letter  
        else if (Character.isLowerCase(chr))
            lowerCount++;
        
        //Check if there are no more than 12 characters
        else if (Character.isDigit(chr))
            maxEl++;
        
        //Check if there are at least 7 characters
        else if (Character.isDigit(chr))
            minEl++;        
        
        
    }
    
    
    
    //If the password is valid, print "Password valid and accepted"
    if(upperCount < MIN_Uppercase
        && lowerCount < MIN_Lowercase
        && maxEl < MAX
        && minEl < MIN
        && specialX !="!")
        System.out.println("Password valid and\r\n"+ "accepted");
    
    // If the password isn’t valid, print "Error"
    else {
        System.out.println("Error!  ");
            
            
            
    } 
    
    
    
    
    

}

}

I'm new at Java, and my first assignment is building a password validator. I'm not sure if it's the operators or the way I set up the strings. I can't seem to get the validation right. Also, how do I Validate special characters (!%$#...) without using regex.

import java.util.Scanner;

public class Password {

public static void main(String[] args) {
    
     
     final int MAX=12;
     final int MIN= 7;
     final int MIN_Uppercase=1;
     final int MIN_Lowercase=1;
     final String SPECIAL="!";
     
     int maxEl=0;
     int minEl=0;
     int upperCount=0;
     int lowerCount=0;
     String specialX = "!";
     
     
     
    // Print a message asking the user to input a password via the console 
        System.out.println("The password must contain:\r\n"
                + " At least one lowercase letter\r\n"
                + " At least one uppercase letter\r\n"
                + " At least minimum 7 characters\r\n"
                + " At least maximum 12 characters\r\n"
                + " An exclamation point: !"
                + "Please enter a password: ");
        
    //Receive the input via the console
        Scanner scan = new Scanner(System.in);
        String password = scan.nextLine();
        int passwordLen = password.length();
        
        
    //Check if there is at least one lower case letter  
    for (int i = 0; i < passwordLen; i++ ) {
        char chr = password.charAt(i);
        
        //Check if there is at least one upper case letter
        
        if (Character.isUpperCase(chr))
            upperCount++;
        
        //Check if there is at least one lower case letter  
        else if (Character.isLowerCase(chr))
            lowerCount++;
        
        //Check if there are no more than 12 characters
        else if (Character.isDigit(chr))
            maxEl++;
        
        //Check if there are at least 7 characters
        else if (Character.isDigit(chr))
            minEl++;        
        
        
    }
    
    
    
    //If the password is valid, print "Password valid and accepted"
    if(upperCount < MIN_Uppercase
        && lowerCount < MIN_Lowercase
        && maxEl < MAX
        && minEl < MIN
        && specialX !="!")
        System.out.println("Password valid and\r\n"+ "accepted");
    
    // If the password isn’t valid, print "Error"
    else {
        System.out.println("Error!  ");
            
            
            
    } 
    
    
    
    
    

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文