我正在尝试用Java创建一个检查三个特定输入的程序。它必须通过这些测试:
7
个字符。upper
和lower
个字母字符。1
数字。到目前为止,我已经能够检查是否有7个字符,但我遇到了最后两个问题。作为if语句,我应该在循环中放入什么来检查数字并使其成为大写和小写。任何帮助将不胜感激。 这是我到目前为止所拥有的。
import java.awt.*;
import java.io.*;
import java.util.StringTokenizer;
public class passCheck
{
private static String getStrSys ()
{
String myInput = null; //Store the String that is read in from the command line
BufferedReader mySystem; //Buffer to store the input
mySystem = new BufferedReader (new InputStreamReader (System.in)); //creates a connection to system input
try
{
myInput = mySystem.readLine (); //reads in data from the console
myInput = myInput.trim ();
}
catch (IOException e) //check
{
System.out.println ("IOException: " + e);
return "";
}
return myInput; //return the integer to the main program
}
//****************************************
//main instructions go here
//****************************************
static public void main (String[] args)
{
String pass; //the words the user inputs
String temp = ""; //holds temp info
int stringLength; //length of string
boolean goodPass = false;
System.out.print ("Please enter a password: "); //ask for words
pass = getStrSys (); //get words from system
temp = pass.toLowerCase ();
stringLength = pass.length (); //find length of eveyrthing
while (goodPass == false)
{
if (stringLength < 7)
{
System.out.println ("Your password must consist of at least 7 characters");
System.out.print ("Please enter a password: "); //ask for words
pass = getStrSys ();
stringLength = pass.length ();
goodPass = false;
}
else if (/* something to check for digits */)
{
}
}
答案 0 :(得分:7)
当然,你可以提出一个令人费解的几乎不可读的 -regex这样做,但我不建议。除了可读性方面,如果密码失败,它不会告诉您原因。这解决了这两个问题:
while (true) {
pass = getStrSys();
if (pass.length() < 7) {
System.out.println("must be at least 7 characters long");
} else {
boolean upper = false;
boolean lower = false;
boolean number = false;
for (char c : pass.toCharArray()) {
if (Character.isUpperCase(c)) {
upper = true;
} else if (Character.isLowerCase(c)) {
lower = true;
} else if (Character.isDigit(c)) {
number = true;
}
}
if (!upper) {
System.out.println("must contain at least one uppercase character");
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
} else if (!number) {
System.out.println("must contain at least one number");
} else {
break;
}
}
}
答案 1 :(得分:4)
正则表达式更适合您要完成的任务。例如,使用lookahead assertions,您可以使用以下内容:
Pattern p = Pattern.compile("^.*(?=.{7,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$");
String pass = getStrSys();
Matcher m = p.matcher(pass);
if (m.matches()) {
System.out.println("Valid password.");
} else {
System.out.println("Invalid password.");
}
答案 2 :(得分:1)
答案 3 :(得分:1)
在你的情况下。验证应该有两个条件成立。我认为以下将是实现它的最佳方法。
private static final char[] SPECIAL = "$!+\-#?_%&/".toCharArray();
private static final char[] NUMBER = "0123456789".toCharArray();
public static boolean checkValidation(String password)
{
int points = 0;
String lowerPass = password.toLowerCase();
String upperPass = password.toUpperCase();
if(!password.equals(lowerPass) && !password.equals(upperPass))
{
// if contains upper or lower letter
points++;
}
if(contains(password, SPECIAL))
{
// if it contains special character
points++;
}
if(contains(password, NUMBER))
{
// if it contains Number
points++;
}
return points >= 2;
}
public static boolean contains(String pwd, char[] value)
{
int i = 0;
boolean success = false;
while(i < value.length && !success)
{
if(pwd.indexOf(""+value[i]) != -1)
{
success = true;
}
i++;
}
return success;
}