Duke
Duke

Reputation: 15

Why are my results different from what I want?

First, I will ask the user to type the password, then my code will measure the strength of this password as the following rules:

I have already finished the code but when I type the password is "Hellllllo@123", the result will equal 7 instead of 5. I know my mistake will happen when the password contains the digit and non_aplhanumeric characters but I don't know how to fix it.

    public class passwordDemo
    {
        String password;
        int point;

        //getter and setter
        public int getPoint()
        {
            return point;
        }

        public void setPoint(int point)
        {
            this.point = point;
        }

        public String getPassword()
        {
            return password;
        }

        public void setPassword(String password)
        {
            this.password = password;
        }

        public passwordDemo(String password)
        {
            this.password = password;
        }

        public passwordDemo()
        {
        }

        //main method

        public int point()
        {
            point = 0;
            //length
            if (password.length() >= 8)
            {
                point += 1;
            }
        //upper and lower
            String pass_upper = password.toUpperCase();
            String pass_lower = password.toLowerCase();

            for (int i = 0; i < password.length(); i++) {
                if (password.charAt(i) == pass_upper.charAt(i))
                {
                    point += 1;
                    break;
                }
            }
            for (int k = 0; k < password.length(); k++) {
                if (password.charAt(k) == pass_lower.charAt(k))
                {
                    point += 1;
                    break;
                }
            }
        //digit
            String digit = "0123456789";
            for (int i = 0; i < password.length(); i++) {
                for (int k = 0; k < digit.length(); k++) {
                    if (password.charAt(i) == digit.charAt(k))
                    {
                        point += 1;
                        break;
                    }
                }
            }

            //non-alphabetic
            char[]non_alphabetic = {
                '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-',
                '+'};
            for (int i = 0; i < password.length(); i++) {
                for (int k = 0; k < non_alphabetic.length; k++) {
                    if (password.charAt(i) == non_alphabetic[k])
                    {
                        point += 1;
                        break;
                    }
                }
            }

            return point;
        }
    }
import java.util.Scanner;

public class Strength_password
{
    public static void main(String[]args)
    {
        Scanner sc = new Scanner(System.in);
        passwordDemo password = new passwordDemo();
        System.out.println("Type your password");
        password.setPassword(sc.nextLine());
        System.out.println(password.point());
    }
}

Upvotes: 0

Views: 103

Answers (1)

Abra
Abra

Reputation: 20924

First the code. Explanations appear after the code.

import java.util.Scanner;

public class PasswordDemo {
    String password;
    int point;

    // getter and setter
    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        if (password != null  &&  !password.isEmpty()) {
            this.password = password;
        }
        else {
            throw new IllegalArgumentException("Null or empty password.");
        }
    }

    public PasswordDemo(String password) {
        setPassword(password);
    }

    // main method
    public int point() {
        point = 0;
        // length
        int len = password.length();
        if (len >= 8  &&  len <= 12) {
            point++;
        }
        else if (len > 12) {
            point += 2;
        }
        // uppercase
        if (password.matches("^.*[A-Z].*$")) {
            point++;
        }
        // lowercase
        if (password.matches("^.*[a-z].*$")) {
            point++;
        }
        // digit
        if (password.matches("^.*[0-9].*$")) {
            point++;
        }
        // non-alphabetic
        char[] non_alphabetic = {'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+'};
        for (int i = 0; i < password.length(); i++) {
            boolean found = false;
            for (int k = 0; k < non_alphabetic.length; k++) {
                if (password.charAt(i) == non_alphabetic[k]) {
                    point++;
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }
        return point;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Type your password");
        PasswordDemo password = new PasswordDemo(sc.nextLine());
        System.out.println(password.point());
    }
}
  • I changed the class name so as to adhere to java naming conventions.
  • I don't think you need a no-argument constructor because member password, of class PasswordDemo, should not be null or empty since it doesn't make sense to have a null or empty password. Remember, you should always write code that handles unexpected data. Hence method setPassword throws an unchecked exception if the method argument is either null or an empty string.
  • Method point
    • Note that point++ is the same as point = point + 1. Similarly, point += 2 is the same as point = point + 2.
    • If the password length is between 8 and 12, then one is added to point. If the password length is not between 8 and 12 but the length is greater than 12, then 2 is added to point.
    • I use regular expressions to check whether the password contains at least one uppercase letter. I also use regular expressions to check whether the password contains a lowercase letter and whether it contains a digit. The regular expression for the uppercase letter check is as follows.
      1. ^ means the start of the password
      2. . means any character and * means zero or more times, so .* means zero or more characters.
      3. [A-Z] means a single, uppercase letter of the English alphabet.
      4. $ means the end of the password.

Hence the regular expression searches for an uppercase letter in the password. I assume you can now understand the other two regular expressions. ^.*[a-z].*$ searches for a lowercase letter and ^.*[0-9].*$ searches for a digit.

Finally, the test for a non-alphanumeric character uses your code that iterates through the characters of the password and for each character, it iterates through the list of non-alphanumeric characters. As mentioned in the comments to your question, a break only exits the loop it is in. Since the break is in a nested loop, it only exits the inner loop. I use a boolean variable to test whether I need to break in the outer loop. This is not the only way but it is the way I prefer. Another way (as mentioned in the comments to your question) is to use a label. Note that there is no "best" way. Decide which way you prefer and use it. Just remember that, as a professional programmer, other programmers may need to make changes to your code. Hence it is important that the code is written in a way that other programmers can easily read and understand.

According to the rules (as I understand them and as I have implemented them in the above code), if the password is

Hellllllo@123

then its point value is 6.

  • The length is greater than 12 so two points.
  • It contains an uppercase letter so another one point.
  • It contains a lowercase letter so another one point.
  • It contains a digit so another one point.
  • It contains a non-alphanumeric character so another one point.

The value is therefore

2 + 1 + 1 + 1 + 1

So unless I did not understand the rules, you are mistaken if you think that the point value of Hellllllo@123 is 5.

Upvotes: 1

Related Questions