简体   繁体   English

android studio 中的 JAVA 从未使用过分配的值 true

[英]JAVA in android studio The value true assigned is never used

I wrote A method to check that the password A user enters when registering to the app is valid.我写了一个方法来检查用户在注册应用时输入的密码是否有效。 A valid password will be with length of 8-16 charachters and contains only numbers,uppercase and lowercase letters.有效密码长度为 8-16 个字符,仅包含数字、大写和小写字母。

according to android studio in the For loop when the value is OK it will never assign the value true to the variables and I can't understand why.根据 For 循环中的 android 工作室,当值确定时,它永远不会将值 true 分配给变量,我不明白为什么。

Is it becuase I am trying to work directly with EditText or am I just doing it wrong?是因为我试图直接使用 EditText 还是我做错了?

This is the method:这是方法:

boolean PasswordTest(EditText password){//function to check that the password is OK
        boolean upLetter=false,lowLetter=false,digit=false;
        int i;
        if(password.length()<8||password.length()>16){
            Toast.makeText(getApplicationContext(),"Password must be between 8-16 characters",Toast.LENGTH_LONG).show();
            return false;
        }
        for(i=0;i<password.getText().length();i++) {
            if (Character.isDigit(password.getText().charAt(i))) {
                digit = true;     //The value true assigned to digit is never used
            } else if (Character.isUpperCase(password.getText().charAt(i))) {
                upLetter = true;  //The value true assigned to upLetter is never used
            } else if (Character.isLowerCase(password.getText().charAt(i))) {
                lowLetter = true; //The value true assigned to lowLetter is never used
            } else {
                Toast.makeText(getApplicationContext(),"Password must contain uppercase,lowercase and numbers",Toast.LENGTH_LONG).show();
                return false;
            }
        }
        Toast.makeText(getApplicationContext(),"all data is correct",Toast.LENGTH_LONG).show();
        return true;
    }

thanks for the help谢谢您的帮助

according to android studio in the For loop when the value is OK it will never assign the value true to the variables and I can't understand why.根据 For 循环中的 android 工作室,当值确定时,它永远不会将值 true 分配给变量,我不明白为什么。

You misread the warning.你误读了警告。

Your code is assigning values to these 3 local variables (or well, it should do that, depending on the input data).您的代码正在为这 3 个局部变量赋值(或者,它应该这样做,具体取决于输入数据)。

But then the method ends, and these 3 local variables cease to exist.但随后方法结束,这 3 个局部变量不复存在。

The warning tells you that these assignments are meaningless because there is no other code following that uses the 3 local variables for anything.警告告诉你这些赋值是没有意义的,因为后面没有其他代码使用这 3 个局部变量来做任何事情。

In other words: you have to step back and ask yourself: "okay, now that the loop is over, how should I use these 3 local variables to do something else".换句话说:你必须退后一步问自己:“好的,既然循环结束了,我应该如何使用这 3 个局部变量来做其他事情”。

So: this has nothing to do with the editor you are using.所以:这与您使用的编辑器无关。 The tool tells you that the code you have written "makes no sense".该工具告诉您,您编写的代码“没有意义”。 When you do not use the result of an operation, then why compute it?!当您使用运算结果时,为什么要计算它?!

You could switch this over to regex and make it simple.您可以将其切换为正则表达式并使其变得简单。

Pattern pattern;
Matcher matcher;

final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{4,}$";

boolean PasswordTest(EditText password){//function to check that the password is OK
    
    pattern = Pattern.compile(PASSWORD_PATTERN);
    matcher = pattern.matcher(password);

    return matcher.matches();
}

And then on the method calling the PasswordTest check and then show a Toast message.然后对调用PasswordTest的方法进行检查,然后显示 Toast 消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM