Beginnen Sie mit der Zerlegung Ihres Problems in überschaubare Brocken.
Password should be at least 8 characters in length.
Nun, das ist einfach ...
if (password.length > 7) {
...
should contain at least one number/digit
Character.isDigit(char)
one capital letter other than the first letter (if the only capital letter is the first letter, that doesn’t pass the complexity test)
Character.isUpperCase(char)
Sie erhalten eine zusätzliche Prüfung tun müssen, wenn die Anzahl der Großbuchstaben 1
ist um festzustellen, ob es das erste Zeichen nicht ist ...
if (!Character.isUpperCase(password[0])...
one special character (non-letter; non-digit)
Character.isLetterOrDigit(char)
Spaces are not allowed.
Character.isWhitespace(char)
Sie dies überprüfen müssen, da diese Registerkarten enthält, neue Linien und ein paar andere Leerzeichen
Nun brauchen Sie nur die Logik zusammenfügen.
Die Mehrheit Ihrer Arbeit ist Zeichen und Überprüfung zählen, um zu sehen, ob Sie die richtige Nummer haben oder nicht
Nun, weil ich pedantisch bin, Ich mag mit einer Schnittstelle starten, die den Kern Vertrag definiert ..
.
public interface PasswordValidator {
public boolean isValid(char[] password);
}
und dann eines generieren oder mehrere Implementierungen ...
public class ComplexPasswordValidator implements PasswordValidator {
@Override
public boolean isValid(char[] password) {
boolean isValid = false;
// At least 8 characters long
if (password.length > 7) {
int digitCount = 0;
int capitalCount = 0;
int nonAlphaDigitCount = 0;
int whiteSpace = 0;
for (char c : password) {
if (Character.isDigit(c)) {
digitCount++;
} else if (Character.isUpperCase(c)) {
capitalCount++;
} else if (Character.isWhitespace(c)) {
// This includes tabs, new lines etc...
whiteSpace++;
} else if (!Character.isLetterOrDigit(c)) {
nonAlphaDigitCount++;
}
}
// Must have a captial character which is not the first character
if (capitalCount > 1 || (capitalCount == 1 && !Character.isUpperCase(password[0]))) {
// Must have a digit, 1 non alpha/digit character and no whitespace
if (digitCount > 0 && nonAlphaDigitCount == 1 && whiteSpace == 0) {
isValid = true;
}
}
}
return isValid;
}
}
nun diese werfen keine Ausnahmen, so dass die einzige Feedback werden Sie i erhalten s true
oder false
. Es wäre kein Problem für Sie, Ausnahmen hinzuzufügen, wenn Sie sie benötigen.
Dann könnte man einfach nutzen so etwas wie ...
PasswordValidator pv = new ComplexPasswordValidator();
System.out.println(pv.isValid(new char[]{'T', 'h', 'i', 's', 'I', 's', 'A', 'T', 'e', 's', 't', '_', 'I', 'A', 'm', 'G', 'r', '8'}));
System.out.println(pv.isValid(new char[]{'B', 'a', 'd', '_', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd', '8'}));
System.out.println(pv.isValid(new char[]{'B', 'a', 'd', ' ', 'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '8'}));
Sie können Setup eine Folge von Methoden, die eine Instanz von PasswordValidator
nahm und übergeben Sie einfach in was auch immer Validator wollten Sie
The compiling issue is from the password field....?
Problem # 1 ...
Platzieren Code nach der return
-Anweisung, das macht den Code unwiderruflich
public static boolean checkSSN(String social) {
//...
return valids;
//Code to check password
String password = JOptionPane
.showInputDialog("Please insert a valid password ");
boolean valid = isValid(password);
if (isValid(password)) {
System.out.println("Valid Password");
} else {
System.out.println("Invalid Password");
}
}
Problem # 2 ...
Failing to define the parameter type...
public boolean isValid(password) { // What type is password?
Bitte können Sie den Code in der Frage stellen sich nicht ein Link. –
Es sagt, es ist zu lang – Josh
Lesen Sie dies dann. http://stackoverflow.com/help/mcve –