Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: some error

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default some error

    public class countletters {
        public static boolean letter(char L) {        
            String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";        
            return (letters.indexOf(L) >= 0);
        }
     
        public static boolean Space(char Sp) {        
            String space = " ";        
            return (space.indexOf(Sp) >= 0);
        }
     
        public static void main(String[] args) {
            int letterNo, consNo, splNo, digitNo, passNo, i;
            String password;
     
            System.out.println ("Enter password: ");
            password = Keyboard.readString();
     
        passNo = 0;
            for(i = 0; i<password.length();i++){
                passNo++;
            }
     
        letterNo = 0;
            for (i=0;i<password.length();i++) {
                if (letter(password.charAt(i))) {
                    letterNo ++;
     
                }
            }            
     
        digitNo = 0;
            for (i=0;i<password.length();i++) {
                if (Character.isDigit(password.charAt(i))){
                    digitNo ++;
     
                }
            }        
     
        splNo = 0;
            for (i=0;i<password.length();i++) {
                if (!Character.isLetter(password.charAt(i)) && 
                    !Space((password.charAt(i))) &&
                    !Character.isDigit(password.charAt(i))){
                    splNo ++;
     
                }
            }
            while(passNo<6 && passNo >10 && letterNo>=1 && digitNo >=1 ){
                  System.out.println("Enter password: ");
                   System.out.println("please enter a valid password:");
            Keyboard.readString();
               }
     
     
        }
    }
    hehe^^...I dont usually use while loops ...anything i can do to make this work?


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: some error

    Looking at the code in my IDE all I get is an error saying that it cant find the class Keyboard. What type of error are you getting or what exactly are you experiencing as wrong in this code?

    I take it this is a way of checking to see if the input password is long and strong enough?

    I've created a utility class for this before which can been seen below. This class will also generate a password for you of the given length.

    /**
     * Password generator created for the members of Java Programming Forums. Feel free
     * to use this in your own applications. Please give credit where credit is due.
     *
     * @author Daniel Johansson
     * @since 9 Jul 2009
     */
    public class PasswordUtility {
     
        private static final String ALPHA_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
     
        private static final String DIGITS = "0123456789";
     
        private static final String SYMBOLS = "$%&*@#!_:~";
     
        /**
         * Since this is a utility class there is no need to instantiate it. All statics baby!
         */
        protected PasswordUtility() {
            throw new UnsupportedOperationException();
        }
     
        /**
         * Generates a random password of the specified length using uppercase letters, lowercase letters, digits and symbols.
         *
         * @param length the length of the password.
         * @return character array representing the password
         */
        public static char[] generate(final int length) {
            final String allowedCharaters = ALPHA_CHARACTERS + DIGITS + SYMBOLS;
            final Random random = new Random();
            final char[] password = new char[length];
     
            for (int i = 0; i < length; ++i) {
                password[i] = allowedCharaters.charAt(random.nextInt(allowedCharaters.length() - 1));
            }
     
            return password;
        }
     
        /**
         * Calculates the strength of the password based on certain factors such as.
         * <ul>
         * <li>Length</li>
         * <li>Number of upper case letters</li>
         * <li>Number of lower case letters</li>
         * <li>Number of digits</li>
         * <li>Number of digits or symbols in the middle</li>
         * <li>Any repeating characters</li>
         * <li>Sequences</li>
         *
         * It also adds bonuses if the password is 8 characters or more and it has a certain combination of lowercase,
         * uppercase, digits and symbols.
         *
         * @param password the password to check the strength of
         * @return the strength which is a value between zero and one hundred (0 - 100).
         */
        public static int strength(final String password) {
            int strength = 0;
     
            // Length
            strength += (password.length() * 4);
     
            // Upper case letters
            final int upperCase = matches(password, "[A-Z]");
     
            if (upperCase > 0) {
                strength += ((password.length() - upperCase) * 2);
            }
     
            // Lower case letters
            final int lowerCase = matches(password, "[a-z]");
     
            if (lowerCase > 0) {
                strength += ((password.length() - lowerCase) * 2);
            }
     
            // Numbers
            final int numbers = matches(password, "[0-9]");
     
            if (numbers > 0) {
                strength += (numbers * 4);
            }
     
            if (numbers == password.length()) {
                strength -= numbers;
            }
     
            // Sepcial characters
            final int specialCharacters = matches(password, "[:,!,@,#,$,%,^,&,*,?,_,~]");
     
            if (specialCharacters > 0) {
                strength += (specialCharacters * 6);
            }
     
            // Letters only
            final int letters = matches(password, "[a-z|A-Z]");
     
            if (letters == password.length()) {
                strength -= letters;
            }
     
            // Middle numbers
            int middles = 0;
     
            for (int j = 0; j < DIGITS.length(); ++j) {
                if ((password.indexOf(DIGITS.charAt(j)) > 0) && (password.indexOf(DIGITS.charAt(j)) < password.length() - 1)) {
                    ++middles;
                }
            }
     
            // Middle symbols
            for (int j = 0; j < SYMBOLS.length(); ++j) {
                if ((password.indexOf(SYMBOLS.charAt(j)) > 0) && (password.indexOf(SYMBOLS.charAt(j)) < password.length())) {
                    ++middles;
                }
            }
     
            strength += (middles * 2);
     
            // Repeat charaters
            final List<Character> done = new ArrayList<Character>();
            int repeats = 0;
     
            for (int i = 0; i < password.length(); ++i) {
                int matches = 0;
     
                for (int j = 0; j < password.length(); ++j) {
                    if (password.toLowerCase().charAt(i) == password.toLowerCase().charAt(j) && !done.contains(password.toLowerCase().charAt(i))) {
                        ++matches;
                    }
                }
     
                if (matches > 1) {
                    strength -= (matches * (matches - 1));
                    repeats += (matches * (matches - 1));
                    done.add(password.toLowerCase().charAt(i));
                }
            }
     
            // Consequtive upper case letters
            final int consequtiveUpperCaseLetters = matches(password, "[A-Z]{2}");
            strength -= (consequtiveUpperCaseLetters * 2);
     
            // Consequtive lower case letters
            final int consequtiveLowerCaseLetters = matches(password, "[a-z]{2}");
            strength -= (consequtiveLowerCaseLetters * 2);
     
            // Consequtive numbers
            final int consequtiveNumbers = matches(password, "[0-9]{2}");
            strength -= (consequtiveNumbers * 2);
     
            // Sequential characters
            for (int i = 0; i < password.length(); ++i) {
                final int index = ALPHA_CHARACTERS.indexOf(String.valueOf(password.charAt(i)).toLowerCase());
     
                if (index != -1 && index < ALPHA_CHARACTERS.length() - 3) {
                    final String forwardSequence = ALPHA_CHARACTERS.substring(index, index + 3);
                    final String reverseSequence = new StringBuffer(forwardSequence).reverse().toString();
     
                    if (password.indexOf(forwardSequence) != -1 || password.indexOf(reverseSequence) != -1) {
                        strength -= 3;
                    }
                }
            }
     
            // Sequential numbers
            for (int i = 0; i < password.length(); ++i) {
                final int index = DIGITS.indexOf(String.valueOf(password.charAt(i)).toLowerCase());
     
                if (index != -1 && index < DIGITS.length() - 3) {
                    final String forwardSequence = DIGITS.substring(index, index + 3);
                    final String reverseSequence = new StringBuffer(forwardSequence).reverse().toString();
     
                    if (password.indexOf(forwardSequence) != -1 || password.indexOf(reverseSequence) != -1) {
                        strength -= 3;
                    }
                }
            }
     
            // Bonuses
            int bonus = 0;
     
            if (password.length() >= 8) {
                ++bonus;
     
                if (((double) upperCase / (double) password.length()) >= 0.25) {
                    ++bonus;
                }
     
                if (((double) lowerCase / (double) password.length()) >= 0.25) {
                    ++bonus;
                }
     
                if (((double) numbers / (double) password.length()) >= 0.25) {
                    ++bonus;
                }
                if (((double) specialCharacters / (double) password.length()) >= 0.25) {
                    ++bonus;
                }
            }
     
            strength += (bonus * 2);
     
            if (strength < 0) {
                strength = 0;
            } else if (strength > 100) {
                strength = 100;
            }
     
            return strength;
        }
     
        private static int matches(final String string, final String regexPattern) {
            int matches = 0;
            final Pattern pattern = Pattern.compile(regexPattern);
            final Matcher matcher = pattern.matcher(string);
     
            while (matcher.find()) {
                ++matches;
            }
     
            return matches;
        }
    }

    Enjoy!

    // Json