该代码将输入密码,并检查密码是否包含字母和数字。如果没有密码,则两个注册都将继续。
当我输入字母和数字时,寄存器是完整的;当我输入数字时,只有寄存器会继续;当我输入字母时,只有寄存器是完整的而不包含数字。
import java.io.*;
public class Sample {
static BufferedReader dataIn = new BufferedReader(new
InputStreamReader(System.in));
public static void main (String[] args)throws Exception {
boolean valid = false;
boolean alphaCheck = false;
boolean numCheck = false;
char[] alpha = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char[] num = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
do {
System.out.println("Password must contain Letters and Numbers Only. (abc../ABC.., 123..)");
System.out.println("Special Characters are not Allowed.");
System.out.print("Register Password: ");
String password = dataIn.readLine();
char[] passwordToArray = password.toUpperCase().toCharArray();
// Check if password contains numbers
for(char x: passwordToArray){
for(char y: num){
if(x==y){
numCheck = true;
break;
}
}
}
// Check if password contains Letters
for(char x: passwordToArray){
for(char y: alpha){
if(x==y){
alphaCheck = true;
break;
}
}
}
if(!numCheck){
System.out.println("No Numbers Found.");
}
if(!alphaCheck){
System.out.println("No Letters Found.");
}
// Check if password contains both Alphabets and Numbers
// if false Continue Register
if((numCheck)&&(alphaCheck)){
System.out.println("Register Complete");
valid = true;
}else{
System.out.println("Register Failed. Please Try again");
}
}while (!valid) ;
}
}
相关分类