I am trying to make a program in Java that checks for three specific inputs. It has to have pass these tests:开发者_高级运维
- At least
7
characters. - Contain both
upper
andlower
case alphabetic characters. - Contain at least
1
digit.
So far I have been able to make it check if there is 7 characters, but I am having trouble with the last two. What should I put in my loop as an if statement to check for digits and make it upper and lower case. Any help would be greatly appreciated. Here is what I have so far.
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 */)
{
}
}
Sure you can come up with a convoluted—almost unreadable—regex for doing this but I wouldn't suggest it. Apart from the readability aspect, if the password fails it doesn't tell you why. This solves both of these problems:
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;
}
}
}
A regular expression is more appropriate for what you are trying to accomplish. For instance, using lookahead assertions, you'd use something like this:
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.");
}
- Don't use StringTokenizer. Use String.split.
- Use the functions in the Character class to check for upper case, lower case, or numeric.
- Since you've forced lower case, you can't check. You need to get rid of that.
In your situation. There should be two condition true for validation. I think following will be the best approach to achieve it.
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;
}
精彩评论