0

So I've been working on this password strength checker, and to provide visual feedback of the breakdown of points to the user as the password is being typed in, I use a KeyTyped event and then analyze the string and eventually start giving out points as the minimum length is reached. Here's what the a part of the analysis looks like :

if (in.matches("[a-z]+")){
    lowerPenalty = -15;
}
if (in.matches("[0-9]+")){
    numPenalty = -15;
}

for(int i=0;i<inLen;i++){
    if ((in.charAt(i) + "").matches("[A-Z]")){   
        upperCounter++;
        upperBonus = upperBonus + 4; 
    }

However, when I run the program, it doesn't consider the last character of the password typed in by the user, and thus the corresponding counter is not incremented. Here's the screenshot:

enter image description here

As you can see in the above screenshot, the numCounter in Number Bonus row is still at '1' instead of '2'. I've tried using KeyPressed event, though the problem still persists.

Please help.

As requested, here's the keyListener code:

input.addKeyListener(new KeyAdapter(){
        @Override
        public void keyTyped(KeyEvent e1){

            if ((int)e1.getKeyChar() == 8){
                count = -1;
                baseScore = 0;
                lenBonus = 0;
                upperBonus = 0;
                upperCounter = 0;
                numBonus = 0;
                numCounter = 0;
                symBonus = 0;
                symCounter = 0;
                comBonus = 0;
                lowerPenalty = 0;
                numPenalty = 0;
                comBonus = 0;
                totalScore = 0;
                input.setText("");
                strength_lbl.setText("Enter a random password");
                strength_lbl.setBackground(Color.LIGHT_GRAY);

            }

            count++;
            Counter.setText(count+"");
            analyzeStr(input.getText());

            baseScore_txt.setText(baseScore+""  );
            lowerPen_txt.setText(lowerPenalty+"");
            numonlyPen_txt.setText(numPenalty+"");
            upperBonus_txt.setText(upperBonus+" [" + (upperCounter) + "x4]");
            numBonus_txt.setText(numBonus+" [" + numCounter + "x5]");
            symBonus_txt.setText(symBonus+" [" + symCounter + "x5]");
            comBonus_txt.setText(comBonus+"");
            totalScore = baseScore + lenBonus + upperBonus + numBonus + symBonus + comBonus + lowerPenalty + numPenalty;
            totalScore_txt.setText(totalScore+"");  

            if (totalScore>=1 && totalScore<50){
                strength_lbl.setText("Weak!");
                strength_lbl.setBackground(Color.red);
            }
            if (totalScore>=50 && totalScore<75){
                strength_lbl.setText("Average!");
                strength_lbl.setBackground(Color.orange);
            }
            if (totalScore>=75 && totalScore<100    ){
                strength_lbl.setText("Strong!");
                strength_lbl.setBackground(Color.cyan);
            }
            if (totalScore>=100){
                strength_lbl.setText("Secure!");
                strength_lbl.setBackground(Color.green);
            }


        }
    });

As requested, here's the analyzeString method:

    public void analyzeStr(String str){

    String in = input.getText();
    int inLen = input.getText().length();

    if (count == 1){
        strength_lbl.setBackground(Color.RED);
        strength_lbl.setText("At least 8 characters please!");
    }

    if  (input.getText().length()<8){
        lengthBonus_txt.setText("0");
    }
    else{
        lengthBonus_txt.setText(lenBonus +" [" + (count-8) + "x3]");
    }

    if (count==8){

        baseScore = 50;


        if (in.matches("[a-z]+")){
            lowerPenalty = -15;
        }

        if (in.matches("[0-9]+")){
            numPenalty = -15;
        }

        for(int i=0;i<inLen;i++){
            if ((in.charAt(i) + "").matches("[A-Z]")){
                upperCounter++;
                upperBonus = upperBonus + 4; 
            }

            if ((in.charAt(i) + "").matches("[0-9]")){
                numCounter++;
                numBonus = numBonus + 5;
            }

            if ((in.charAt(i) + "").matches("[!,@,#,$,%,^,&,*,?,_,~]")){
                symCounter++;
                symBonus = symBonus + 5;
            }

        }

    }

    if (count>8){
        lenBonus = lenBonus + 3;
        lengthBonus_txt.setText(lenBonus+" [" + (inLen-7) + "x3]");

        if ((in.charAt(inLen-1) + "").matches("[A-Z]")){
            upperCounter++;
            upperBonus = upperBonus + 4; 
        }

        if ((in.charAt(inLen-1) + "").matches("[0-9]")){
            numCounter++;
            numBonus = numBonus + 5;
        }   

        if ((in.charAt(inLen-1) + "").matches("[!,@,#,$,%,^,&,*,?,_,~]")){
            symCounter++;
            symBonus = symBonus + 5;
        }
    }

    if (count>=8){
        if (in.matches("[A-Z][0-9][!,@,#,$,%,^,&,*,?,_,~]")){
            comBonus = 25;
        }
        if (in.matches("[0-9][A-Z][!,@,#,$,%,^,&,*,?,_,~]")){
            comBonus = 25;
        }
        if (in.matches("[!,@,#,$,%,^,&,*,?,_,~][0-9][A-Z]")){
            comBonus = 25;
        }
        if (in.matches("[!,@,#,$,%,^,&,*,?,_,~][A-Z][0-9]")){
            comBonus = 25;
        }
        if (in.matches("[!,@,#,$,%,^,&,*,?,_,~][A-Z][0-9]")){
            comBonus = 25;
        }
        if (in.matches("[A-Z][!,@,#,$,%,^,&,*,?,_,~][0-9]")){
            comBonus = 25;
        }
        if (in.matches("[0-9][!,@,#,$,%,^,&,*,?,_,~][A-Z]")){
            comBonus = 25;
        }
    }


}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Manas Bajaj
  • 1,133
  • 3
  • 16
  • 26
  • please post your KeyListener – Maxim Shoustin Oct 31 '13 at 20:54
  • 1
    Where are you setting `inLen`? – Jon Skeet Oct 31 '13 at 20:55
  • @JonSkeet, in my analyzeString method. Here it is: int inLen = input.getText().length(); – Manas Bajaj Oct 31 '13 at 20:59
  • It would help if you could provide a short piece of compilable code which illustrates the problem. (see sscce.org) – George Tomlinson Oct 31 '13 at 21:04
  • It seems that we will need the entire `analyzeString` – PM 77-1 Oct 31 '13 at 21:04
  • 2
    Ideally, just come up with a short but *complete* program demonstrating the problem. It shouldn't even need to have a UI, to be honest. But there's no point in us having to ask for more and more information, a bit at a time. – Jon Skeet Oct 31 '13 at 21:07
  • I would recommend you to use Character.isDigit, Character.isUpperCase, etc instead of most of those regexes. You get a more reliable, a more readable and possibly even a more correct solution. – kviiri Oct 31 '13 at 21:10
  • @JonSkeet, I'm sorry for that, here's the jar file which clearly portrays the problem: http://www13.zippyshare.com/v/69720042/file.html – Manas Bajaj Oct 31 '13 at 21:11
  • 2
    Don't use a KeyListener. There are ways to enter text in a text field without using the keyboard. Use a DocumentListener. – JB Nizet Oct 31 '13 at 21:11
  • To ask 2 obvious questions, what happens if you run the for loop/loops until inLen+1? Have you printed out the characters it *does* read to check they are what you expect? – George Tomlinson Oct 31 '13 at 21:14
  • 1
    @bluesh34, OutOfBoundsException. That was the first thing I tried : ) – Manas Bajaj Oct 31 '13 at 21:16
  • What about question 2? +1 for info. – George Tomlinson Oct 31 '13 at 21:18
  • @kviiri, getting the same error even with those functions. – Manas Bajaj Oct 31 '13 at 21:23
  • 2
    Please re-read Jon Skeet's last post. He's not asking for the complete program, but rather a new program, one that is *short but complete*, that demonstrates the problem. Else we'd drown in a ton of unrelated code. – Hovercraft Full Of Eels Oct 31 '13 at 21:24
  • @bluesh34, so it starts printing after the second char has been typed in. Here's the screen shot: http://speedcap.net/sharing/screen.php?id=files/55/e0/55e0a90496f3ba13152cc124c6749d27.png – Manas Bajaj Oct 31 '13 at 21:27
  • Why doesn't it start printing after the 1st char is typed? – George Tomlinson Oct 31 '13 at 21:29
  • @bluesh34, I have no idea. It starts incrementing the lengthCounter from the first char, but prints from the second char. – Manas Bajaj Oct 31 '13 at 21:34
  • @EnTHuSiAsTx94, yes, I wasn't actually thinking that a faulty regex was causing the problem. I just wanted to let you know about these methods since they are more concise, more readable and more reliable. isUpperCase is also better than [A-Z] because it also recognizes uppercase characters beyond A-Z, such as scandics. – kviiri Oct 31 '13 at 21:40
  • Which classes are the code stubs posted in? – George Tomlinson Oct 31 '13 at 21:44
  • @kviiri, oh, thanks for your help. I actually managed to fix the problem using KeyPressed event instead. However, now all key presses are detected even though they don't print anything. Any idea how to fix this i.e. restrict the key presses to only printable characters? – Manas Bajaj Oct 31 '13 at 21:44
  • Maybe this will help with the printable characters only: http://stackoverflow.com/questions/4179708/how-to-detect-if-the-pressed-key-will-produce-a-character-inside-an-input-text – George Tomlinson Oct 31 '13 at 21:50
  • 1
    `KeyListener` isn't really your best choice here, `DocumentListener` would be a better choice, but without a runnable example, it's very hard to decipher what's going on... – MadProgrammer Oct 31 '13 at 23:53

0 Answers0