简体   繁体   中英

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.

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.

Is it becuase I am trying to work directly with EditText or am I just doing it wrong?

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.

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).

But then the method ends, and these 3 local variables cease to exist.

The warning tells you that these assignments are meaningless because there is no other code following that uses the 3 local variables for anything.

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".

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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