This code will Input password and check if the password contains letters and numbers. If password don't have both register will continue.
此代码将输入密码并检查密码是否包含字母和数字。如果密码没有,则两个寄存器将继续。
When I enter letters and numbers the register is complete, when I enter numbers only the register continue, and when I enter letters only the register is complete without containing numbers.
当我输入字母和数字时,寄存器完成,当我输入数字时,只有寄存器继续,当我输入字母时,只有寄存器完整而不包含数字。
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) ;
}
}
3 个解决方案
#1
2
You declare (and initialize) your validation variables once before the loop, but you might loop multiple times. First with letters, then with only numbers. Or in the reverse order. Regardless, you should reset those sentinel values in the loop body. Something like
您在循环之前声明(并初始化)验证变量一次,但您可能会循环多次。首先是字母,然后只有数字。或者以相反的顺序。无论如何,您应该在循环体中重置这些标记值。就像是
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 {
valid = alphaCheck = numCheck = false;
I would also recommend you write a function to check if the value is present in your array of allowed values. That could make the rest of the code much easier to read,
我还建议您编写一个函数来检查该值是否存在于允许值数组中。这可以使代码的其余部分更容易阅读,
private static boolean contains(char[] arr, char ch) {
for (char v : arr) {
if (ch == v) {
return true;
}
}
return false;
}
And, we can potentially make the array initializations prettier by using String.toCharArray()
. Like,
而且,我们可以通过使用String.toCharArray()来使数组初始化更漂亮。喜欢,
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
char[] num = "1234567890".toCharArray();
#2
0
The problem here is that you never set the value of the boolean to its original state, which should be false, inside the do while loop.
这里的问题是你永远不会在do while循环中将boolean的值设置为其原始状态,该状态应为false。
So let's say you insert a password with just numbers, the password is not passed the first time because it's not valid, but the boolean values numCheck will be true for each future iteration, and of course the same applies if you input just chars.
因此,假设您输入的密码只包含数字,密码不会在第一次传递,因为它无效,但布尔值numCheck对于每个将来的迭代都将为true,当然,如果您只输入字符,则同样适用。
This program will pass the inserted password in three cases then
此程序将在三种情况下传递插入的密码
-
If you first insert a password with just letters and then you insert a password with just numbers
如果您首先插入只有字母的密码,然后插入一个只有数字的密码
-
if you first insert a password with just numbers and then you insert a password with just letters
如果您首先插入一个只有数字的密码,然后插入一个只有字母的密码
-
if you type the correct password with both numbers and letters
如果您输入带有数字和字母的正确密码
This is how you should correct your code.
这是你应该如何纠正你的代码。
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;
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 {
boolean alphaCheck = false;
boolean numCheck = false;
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();
System.out.println(passwordToArray);
// 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) {
System.out.println(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);
}
}
#3
0
A solid alternative solution for password validation is to simply check if the ascii code of each letter is within the proper range. Then there's no need to define any charArray or Regex and also could do the checks with very primitive arithmetic functions! You can find the ASCII reference values everywhere in internet and in Java for each letter (Character or String) using .hashCode()
method. This is especially helpful, when you need to fulfill custom requirements for a valid password, like at least one special character, letter at the beginning, 2 numbers etc…
密码验证的可靠替代解决方案是简单地检查每个字母的ascii代码是否在适当的范围内。然后就不需要定义任何charArray或Regex,也可以使用非常原始的算术函数进行检查!您可以使用.hashCode()方法在Internet和Java中的每个字母(字符或字符串)中找到ASCII参考值。当您需要满足有效密码的自定义要求时,这尤其有用,例如至少一个特殊字符,开头的字母,2个数字等...
e.g. you wanna check if a character is within uppercase letters: …
例如你想检查一个字符是否在大写字母内:...
Character c = 'C';
if (c.hashCode() > 64 && c.hashCode() < 91){
...
}
#1
2
You declare (and initialize) your validation variables once before the loop, but you might loop multiple times. First with letters, then with only numbers. Or in the reverse order. Regardless, you should reset those sentinel values in the loop body. Something like
您在循环之前声明(并初始化)验证变量一次,但您可能会循环多次。首先是字母,然后只有数字。或者以相反的顺序。无论如何,您应该在循环体中重置这些标记值。就像是
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 {
valid = alphaCheck = numCheck = false;
I would also recommend you write a function to check if the value is present in your array of allowed values. That could make the rest of the code much easier to read,
我还建议您编写一个函数来检查该值是否存在于允许值数组中。这可以使代码的其余部分更容易阅读,
private static boolean contains(char[] arr, char ch) {
for (char v : arr) {
if (ch == v) {
return true;
}
}
return false;
}
And, we can potentially make the array initializations prettier by using String.toCharArray()
. Like,
而且,我们可以通过使用String.toCharArray()来使数组初始化更漂亮。喜欢,
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
char[] num = "1234567890".toCharArray();
#2
0
The problem here is that you never set the value of the boolean to its original state, which should be false, inside the do while loop.
这里的问题是你永远不会在do while循环中将boolean的值设置为其原始状态,该状态应为false。
So let's say you insert a password with just numbers, the password is not passed the first time because it's not valid, but the boolean values numCheck will be true for each future iteration, and of course the same applies if you input just chars.
因此,假设您输入的密码只包含数字,密码不会在第一次传递,因为它无效,但布尔值numCheck对于每个将来的迭代都将为true,当然,如果您只输入字符,则同样适用。
This program will pass the inserted password in three cases then
此程序将在三种情况下传递插入的密码
-
If you first insert a password with just letters and then you insert a password with just numbers
如果您首先插入只有字母的密码,然后插入一个只有数字的密码
-
if you first insert a password with just numbers and then you insert a password with just letters
如果您首先插入一个只有数字的密码,然后插入一个只有字母的密码
-
if you type the correct password with both numbers and letters
如果您输入带有数字和字母的正确密码
This is how you should correct your code.
这是你应该如何纠正你的代码。
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;
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 {
boolean alphaCheck = false;
boolean numCheck = false;
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();
System.out.println(passwordToArray);
// 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) {
System.out.println(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);
}
}
#3
0
A solid alternative solution for password validation is to simply check if the ascii code of each letter is within the proper range. Then there's no need to define any charArray or Regex and also could do the checks with very primitive arithmetic functions! You can find the ASCII reference values everywhere in internet and in Java for each letter (Character or String) using .hashCode()
method. This is especially helpful, when you need to fulfill custom requirements for a valid password, like at least one special character, letter at the beginning, 2 numbers etc…
密码验证的可靠替代解决方案是简单地检查每个字母的ascii代码是否在适当的范围内。然后就不需要定义任何charArray或Regex,也可以使用非常原始的算术函数进行检查!您可以使用.hashCode()方法在Internet和Java中的每个字母(字符或字符串)中找到ASCII参考值。当您需要满足有效密码的自定义要求时,这尤其有用,例如至少一个特殊字符,开头的字母,2个数字等...
e.g. you wanna check if a character is within uppercase letters: …
例如你想检查一个字符是否在大写字母内:...
Character c = 'C';
if (c.hashCode() > 64 && c.hashCode() < 91){
...
}