Java KeyTyped事件-无法读取字符串的最后一个字符

Java KeyTyped事件-无法读取字符串的最后一个字符,java,string,awt,keylistener,Java,String,Awt,Keylistener,因此,我一直在研究这个密码强度检查器,为了在输入密码时向用户提供点数细分的视觉反馈,我使用了一个KeyTyped事件,然后分析字符串,最终在达到最小长度时开始给出点数。以下是分析的a部分的内容: if (in.matches("[a-z]+")){ lowerPenalty = -15; } if (in.matches("[0-9]+")){ numPenalty = -15; } for(int i=0;i<inLen;i++){ if ((in.charAt

因此,我一直在研究这个密码强度检查器,为了在输入密码时向用户提供点数细分的视觉反馈,我使用了一个KeyTyped事件,然后分析字符串,最终在达到最小长度时开始给出点数。以下是分析的a部分的内容:

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.matches(“[a-z]+”)){
lowerPenalty=-15;
}
if(in.matches(“[0-9]+”)){
numPenalty=-15;
}
对于(int i=0;i=1&&totalScore=50&&totalScore=75&&totalScore=100){
强度设置文本(“安全!”);
强度_lbl.挫折地面(颜色:绿色);
}
}
});
根据要求,以下是analyzeString方法:

    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;
        }
    }


}
public void analyzeStr(字符串str){
字符串in=input.getText();
int inLen=input.getText().length();
如果(计数=1){
强度-立根面(颜色:红色);
strength_lbl.setText(“请至少8个字符!”);
}
if(input.getText().length()=8){
如果(在匹配项中(“[A-Z][0-9][!,@,#,$,%,^,&,*,?,35;,~]”){
comBonus=25;
}
如果(在匹配项中(“[0-9][A-Z][!,@,#,$,%,^,&,*,?,35;,~]”){
comBonus=25;
}
如果(在匹配项中(“[!,@,#,$,%,^,&,*,?,,~][0-9][A-Z]”){
comBonus=25;
}
如果(在匹配项中(“[!,@,#,$,%,^,&,*,?,,~][A-Z][0-9]”){
comBonus=25;
}
如果(在匹配项中(“[!,@,#,$,%,^,&,*,?,,~][A-Z][0-9]”){
comBonus=25;
}
如果(在匹配项中(“[A-Z][!,@,#,$,%,^,&,*,?,",~][0-9]”){
comBonus=25;
}
如果(在匹配项中(“[0-9][!,@,#,$,%,^,&,*,?,",~][A-Z]”){
comBonus=25;
}
}
}

您在哪里设置
inLen
?理想情况下,只需拿出一个简短但完整的程序来演示问题。老实说,它甚至不需要UI。但是我们没有必要一次一点地询问越来越多的信息,不要使用键盘监听器。有几种方法可以在不使用键盘的情况下在文本字段中输入文本。使用DocumentListener。@bluesh34,OutOfBoundsException。这是我尝试的第一件事:)请重读乔恩·斯基特的最后一篇文章。他不是要完整的程序,而是要一个新的程序,一个简短但完整的程序,来证明这个问题。否则我们会被大量无关的代码淹没。
    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;
        }
    }


}