如何在 Java 中检查密码验证中的特殊字符?
How can I check for special characters in password validation in Java?
我正在尝试获取代码来检查是否输入了特殊字符,但是,当密码中未输入特殊字符时,我没有收到任何验证错误。是否有未识别特殊字符的原因?所有其他密码验证检查工作正常
//package sampleProject;
import java.util.*;
import java.util.Scanner;
public class Lab7 {
public static void main(String []args){
// Specify the maximum number of letters in a password
final int MAX=10;
int invalidCount = 0;
// Specifying the number of uppercase letters in password
final int MIN_Uppercase=1;
// Specifying the minimum lowercase letters in password
final int NUM_Digits=2;
// Specify the minimum number of special case letters
final int Special=1;
// Count number of uppercase letters in a password
int uppercaseCounter=0;
// Count digits in a password
int digitCounter=0;
// count special case letters in a password
int specialCounter=0;
System.out.println("Enter the password\n");
Scanner input = new Scanner(System.in);
String password = input.nextLine();
for (int i=0; i < password.length(); i++ ) {
char c = password.charAt(i);
if(Character.isUpperCase(c))
uppercaseCounter++;
else if(Character.isDigit(c))
digitCounter++;
if(c == '$' || c == '#' || c == '?' || c == '!' || c == '_' || c == '=' || c == '%'){
specialCounter++;
}
}
if (password.length() >= MAX && uppercaseCounter >= MIN_Uppercase && specialCounter == 1 && digitCounter == 2 || digitCounter == 3) {
System.out.println("Valid Password");
}
else {
System.out.println("Your password does not contain the following:");
if(password.length() < MAX)
System.out.println("Enter atleast 10 characters");
if (uppercaseCounter < MIN_Uppercase)
System.out.println("Enter at least 1 uppercase character");
if(digitCounter != 2 || digitCounter != 3)
System.out.println("Enter either 2 or 3 digits");
if(specialCounter > 1)
System.out.println("Password should contain only 1 special characters");
}
}
}
当没有输入特殊字符时,您不会收到任何验证错误,因为这不是您的检查所做的:
if(specialCounter > 1)
System.out.println("Password should contain only 1 special characters");
当有多个特殊字符(这没有意义)时,您将打印一条错误消息。我建议:
if(specialCounter < 1)
System.out.println("Password should contain at least 1 special characters");
你应该在这一行中做什么:
if(specialCounter != 1)
System.out.println("Password should contain only 1 special characters");
如果不等于0,则抛出异常。 != 表示不相等,所以如果它小于或大于 1,就会出错。
我正在尝试获取代码来检查是否输入了特殊字符,但是,当密码中未输入特殊字符时,我没有收到任何验证错误。是否有未识别特殊字符的原因?所有其他密码验证检查工作正常
//package sampleProject;
import java.util.*;
import java.util.Scanner;
public class Lab7 {
public static void main(String []args){
// Specify the maximum number of letters in a password
final int MAX=10;
int invalidCount = 0;
// Specifying the number of uppercase letters in password
final int MIN_Uppercase=1;
// Specifying the minimum lowercase letters in password
final int NUM_Digits=2;
// Specify the minimum number of special case letters
final int Special=1;
// Count number of uppercase letters in a password
int uppercaseCounter=0;
// Count digits in a password
int digitCounter=0;
// count special case letters in a password
int specialCounter=0;
System.out.println("Enter the password\n");
Scanner input = new Scanner(System.in);
String password = input.nextLine();
for (int i=0; i < password.length(); i++ ) {
char c = password.charAt(i);
if(Character.isUpperCase(c))
uppercaseCounter++;
else if(Character.isDigit(c))
digitCounter++;
if(c == '$' || c == '#' || c == '?' || c == '!' || c == '_' || c == '=' || c == '%'){
specialCounter++;
}
}
if (password.length() >= MAX && uppercaseCounter >= MIN_Uppercase && specialCounter == 1 && digitCounter == 2 || digitCounter == 3) {
System.out.println("Valid Password");
}
else {
System.out.println("Your password does not contain the following:");
if(password.length() < MAX)
System.out.println("Enter atleast 10 characters");
if (uppercaseCounter < MIN_Uppercase)
System.out.println("Enter at least 1 uppercase character");
if(digitCounter != 2 || digitCounter != 3)
System.out.println("Enter either 2 or 3 digits");
if(specialCounter > 1)
System.out.println("Password should contain only 1 special characters");
}
}
}
当没有输入特殊字符时,您不会收到任何验证错误,因为这不是您的检查所做的:
if(specialCounter > 1)
System.out.println("Password should contain only 1 special characters");
当有多个特殊字符(这没有意义)时,您将打印一条错误消息。我建议:
if(specialCounter < 1)
System.out.println("Password should contain at least 1 special characters");
你应该在这一行中做什么:
if(specialCounter != 1)
System.out.println("Password should contain only 1 special characters");
如果不等于0,则抛出异常。 != 表示不相等,所以如果它小于或大于 1,就会出错。