Hi guys. im writing a program to check an input string (a password) for its level of security (strong, medium, weak). here is my code for the method:

 public static int passwordType(String password) {

//0weak, 1medium, 2strong
int hasSix = 0;
int upper = 0;
int lower = 0;
int digit = 0;

//check for 6 chars
if (password.length() >= 6){
    hasSix = 1;
}

//check for chars and numbers
for (int i=0; i<password.length(); i++){
    upper = 0;
    lower = 0;
    digit = 0;

    if (Character.isUpperCase(password.charAt(i))){
       upper = 1;
       }

     if (Character.isLowerCase(password.charAt(i))){
       lower = 1;
       }   

     if (Character.isDigit(i)){
         digit = 1;
     }

}
//add the scores up and return the appropriate int (0,1,2)
int sum;
sum = upper + lower + digit + hasSix;

int result = 0;

if (sum == 4){
    result= 2;}
if (sum == 3){
    result= 1;}
if (sum <= 2){
    result= 0;}

   return result; 

so as you can see this method should return a 0 for weak, 1 for medium and 2 for strong. however this doesnt work for some reason. it always returns 0. can anyone point me in the right direction? much thanks.

Don't reset upper, lower, and digit inside the loop. You only care that any character matches, so after it's set to 1, that's a match for the whole string. If none of the characters match then it'll remain 0 regardless.

got it thanks for the tip. at this point, it looks like its still not producing the results needed.

Enter input filename: passwords
password: dog Type: 0
password: cat Type: 0
password: password Type: 0
password: eE9402fos Type: 1
password: password1 Type: 0

Number of passwords read:   5
Number of strong passwords: 0
Number of medium passwords: 1
Number of weak passwords:   4

looks like its still not producing the results needed.

Then start up a debugger and trace through the function. Or you can add debug prints that tell you what each value is at key points in the algorithm. It's important to learn how to debug your code, because someone won't always be around to tell you what the problem is and how to fix it. ;)

thanks much for the help & direction. i found the error:

Character.isDigit(i)

needed to be:

Character.isDigit(password.charAt(i))

thanks again all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.