For my password strength checker, I am unable to display the score that I get from the password that I enter. Here is the code below, the problem is displayed all the way at the bottom of my code:
JavaScript
x
import java.lang.Thread.State;
import java.util.Scanner;
public class PasswordUtils {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Password to test: ");
String pw = sc.nextLine();
if (containsUpperCase(pw)) {
System.out.println(" ...contains an upper case letter");
}
if (containsLowerCase(pw)) {
System.out.println(" ...contains an lower case letter");
}
if (containsDigit(pw)) {
System.out.println(" ...contains a number");
}
if (containsSpecial(pw)) {
System.out.println(" ...contains a number");
}
sc.close();
}
/** * Determine if the given string contains an upper case letter * @param s the string to check * @return true if and only if s contains an upper case letter */
JavaScript
public static boolean containsUpperCase(String s) {
for (int letter=0; letter<s.length(); letter++) {
if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(s.charAt(letter))>=0) {
return true;
}
}
return false;
}
public static boolean containsLowerCase(String s) {
for (int letter=0; letter<s.length(); letter++) {
if ("abcdefghijklmnopqrstuvwxyz".indexOf(s.charAt(letter))>=0) {
return true;
}
}
return false;
}
public static boolean containsDigit(String s) {
for (int letter=0; letter<s.length(); letter++) {
if ("1234567890".indexOf(s.charAt(letter))>=0) {
return true;
}
}
return false;
}
JavaScript
public static boolean containsSpecial(String s) {
for (int letter=0; letter<s.length(); letter++) {
if ("!@#$%^&*()_-+=[]{};:,.<>?/|".indexOf(s.charAt(letter))>=0) {
return true;
}
}
return false;
}
JavaScript
/**
* Determine the actual strength of a password based upon various tests
* @param s the password to evaluate
* @return the strength (on a 1 to 5 scale, 5 is very good) of the password
*/
public static int score(String s) {
int pwscore = 0;
//if it contains one digit, add 1 to total score
if( s.matches("(?=.*[0-9]).*") )
pwscore += 1;
//if it contains one lower case letter, add 1 to total score
if( s.matches("(?=.*[a-z]).*") )
pwscore += 1;
//if it contains one upper case letter, add 1 to total score
if( s.matches("(?=.*[A-Z]).*") )
pwscore += 1;
//if it contains one special character, add 1 to total score
if( s.matches("(?=.*[~!@#$%^&*()_-]).*") )
pwscore += 1;
System.out.println("The password strength is " + pwscore);
return pwscore; // Right now, all passwords stink!!!
}
}
Advertisement
Answer
Instead of re-implementing the tests, and you say you already have functional code – I’d prefer that, like –
JavaScript
public static int score(String pw) {
int score = 1; // <-- 1 to 5...
if (containsUpperCase(pw)) {
score++;
}
if (containsLowerCase(pw)) {
score++;
}
if (containsDigit(pw)) {
score++;
}
if (containsSpecial(pw)) {
score++;
}
return score;
}
And then call it and print the result in main
like
JavaScript
System.out.println("The score is " + score(pw));