In: Computer Science
I don't understand why this method will not return true, even when my parameters for boolean results are met, can anyone help shed some light on my mistake?
import java.util.*;
public class PasswordChecker{
public static void main (String[]args){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a password that is 8 characters in
length.");
System.out.println("The password must have at least 3 uppercase
letters,");
System.out.println("3 numeric digits, as well as 2 lowercase
letters,");
System.out.println("and contain no special characters.");
System.out.println();
System.out.println("Enter a password:");
String password = scan.nextLine();
boolean result = checkPassword(password); // main method's call to
the method named checkPassword
if(result){
System.out.println(password + " is Valid.");}
else
System.out.println(password + " is Invalid.");
}
public static boolean checkPassword(String
password){
int digit = 0; int upper = 0; int lower = 0; int n = 0; int old =
0;
boolean digits = digit >=3;
boolean uppers = upper >=2;
boolean lowers = lower >=3;
boolean only = old < 1;
boolean goodLength = password.length() >= 8;
boolean results = digits && uppers && lowers
&& only && goodLength;
while (true){
for (n = 0; n < password.length(); n++) {
if (Character.isDigit(password.charAt(n))){
digit++;}
if (Character.isLowerCase(password.charAt(n))){
lower++;}
if (Character.isUpperCase(password.charAt(n))){
upper++;}
if (!Character.isDigit(password.charAt(n)) &&
!Character.isLetter(password.charAt(n))){
old++;}
}
System.out.println(upper);
System.out.println(lower);
System.out.println(digit);
System.out.println(old);
if (results){
return true;}
if (!results){
return false;}
}
}
}
There are two mistakes.
1) The following check should be done after for loop ends because only after for loop ends the variables digit,upper lower old will contain actual values computed from the string.
boolean digits = digit >=3;
boolean uppers = upper >=2;
boolean lowers = lower >=3;
boolean only = old < 1;
boolean goodLength = password.length() >= 8;
boolean results = digits && uppers && lowers
&& only && goodLength;
2) In the password requirements the following is there but in checking upper and lower case checks are written wrongly, The password must have at least 3 uppercase letters,3 numeric digits, as well as 2 lowercase letters.
boolean uppers = upper >=2;
boolean lowers = lower >=3;
I have modified the code. Please find it below.
import java.util.*;
public class PasswordChecker{
public static void main (String[]args){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a password that is 8 characters
in length.");
System.out.println("The password must have at least 3 uppercase
letters,");
System.out.println("3 numeric digits, as well as 2 lowercase
letters,");
System.out.println("and contain no special characters.");
System.out.println();
System.out.println("Enter a password:");
String password = scan.nextLine();
boolean result = checkPassword(password); // main method's call to
the method named checkPassword
if(result){
System.out.println(password + " is Valid.");}
else
System.out.println(password + " is Invalid.");
}
public static boolean checkPassword(String password){
int digit = 0; int upper = 0; int lower = 0; int n = 0; int old =
0;
while (true){
for (n = 0; n < password.length(); n++) {
if (Character.isDigit(password.charAt(n))){
digit++;}
if (Character.isLowerCase(password.charAt(n))){
lower++;}
if (Character.isUpperCase(password.charAt(n))){
upper++;}
if (!Character.isDigit(password.charAt(n)) &&
!Character.isLetter(password.charAt(n))){
old++;}
}
boolean digits = digit >=3;
boolean uppers = upper >=3;
boolean lowers = lower >=2;
boolean only = old < 1;
boolean goodLength = password.length() >= 8;
boolean results = digits && uppers && lowers
&& only && goodLength;
System.out.println(upper);
System.out.println(lower);
System.out.println(digit);
System.out.println(old);
if (results){
return true;}
if (!results){
return false;}
}
}
}
OUTPUT:
Please enter a password that is 8 characters in length.
The password must have at least 3 uppercase letters,
3 numeric digits, as well as 2 lowercase letters,
and contain no special characters.
Enter a password:
A1B2C3de
3
2
3
0
A1B2C3de is Valid.