简体   繁体   中英

Most efficient way to check all conditions in if statement?

So I'm trying to do this if statement for multiple conditions where in order for the password to qualify it should meet 4 out of 5 things. I get an error saying

The operator && is undefined for the argument type(s) boolean, int

Note: the error occurs at the bottom of my method,(if statement error)

      public void passRequirments(){


        int digit,symbol,upper,lower;
        int countDigit, countSymbol,countUpper,countLower;
        countDigit = 0;
        countSymbol= 0;
        countUpper = 0;
        countLower = 0;

        for (int i = 0; i < passChar.length; i++){
        digit = (int)passChar[i] ;
        symbol = (int)passChar[i];
        upper  = (int)passChar[i];
        lower  = (int)passChar[i];

        if(digit >=48 && digit <= 57){
          countDigit = 1;
          System.out.println(countDigit);
        }
        else if(symbol >=32 && symbol <= 47 || symbol >=58 && symbol <= 64 ||
                symbol >=91 && symbol <= 96 || symbol >=123 && symbol <= 126){
          countSymbol = 1;
          if (countSymbol == 1){

            System.out.println("hello");
          }
        }
        else if( upper >=65 &&  upper <= 90){
          countUpper = 1;
        }
        else {
          countLower = 1;
        }

 }
         //this is where i'm running into error

        if(passChar.length  >= 8  && countDigit == 1 && countSymbol && countUpper == 1 ||
          passChar.length >= 8 && countDigit == 1 && countSymbol && countLower == 1 ||
          passChar.length >= 8 && countDigit == 1 && countLower  && countUpper == 1 ||
          passChar.length >= 8 && countSymbol == 1 && countLower && countUpper == 1){

       System.out.println("Password Qualfies!");
       passwordScore = passwordScore + 10;

      }
      else {

       System.out.println("Password Doesn't Qualify!");


      }


  }

In java, the parameter inside if statement should be strictly boolean .

I see that you're checking simply for countSymbol instead it should be countSymbol == 0 or countSymbol > 0 as per your requirement.

if(passChar.length  >= 8  && countDigit == 1 && countSymbol==0 && countUpper == 1 ||
                                                     ^ change this

Make similar changes in other Or conditions as well

If by countSymbol && countUpper == 1 you are trying to say that both countSymbol and countUpper should be equal to 1, that's not how && works. You have to write out both conditions in full: countSymbol == 1 && countUpper == 1 . Same with countLower && countUpper == 1 .

Fun fact: a notation similar to that would have worked if you were writing in COBOL :)

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