如何验证特殊字符,例如 !在Java中不使用正则表达式的密码?
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论