ID check char Method?

jcruz19 :

I'm stuck in my code I have everything initialize I need to find An employee’s ID is of the length of 8 or more characters and is composed of only capital letters and digits. The first two characters must be capital letters and the ID must contain at least 2 digits. Write a method that takes an employee’s ID as an array of characters and returns true if the ID is valid and false otherwise. I have my code but it won't increment once it finds the first two indexes if they are Capital letters

public static boolean isValidID(char [] id) {      

      if(id.length >= 8)
      {
        validLen = true;
      }
      else if(id.length < 8)
      {
         validLen = false;
      }

      for(int i = 0; i < id.length; i++)
      {  
            if(Character.isUpperCase(0))
            {
               check2Cap++;
            }
            else if(Character.isUpperCase(1))
            {
               check2Cap++;   
            }
            else if(Character.isDigit(i))
            {
               check2Digits++;
            }          

     }  
     if(check2Cap == 2)
     {
        checkCapital = true;
     }
     if(check2Digits >= 2)
     {
        checkDig = true;
     } 
     return validLen && checkCapital && checkDig;
}
RR_IL :

Move the upper case check outside the loop.

Character.isDigit(i) - > Wrong check. You should check against the characters and not the index.


Something like this shall work

  public static boolean isValidID(char [] id) {      

  boolean validStruct = (id.length >= 8) && (Character.isUpperCase(id[0]) && (Character.isUpperCase(id[1]))) ; 
  int numDigits =0 ;

  for(int i = 0; validStruct && i < id.length ; i++)
  {  
     if(Character.isDigit(id[i]))
         numDigits+=1; 
  }  
 return validStruct && (numDigits >= 2);

}


 public static void main(String [] args) throws InterruptedException
    {
     System.out.println(isValidID("FRUMT3UMPO".toCharArray()));
     System.out.println(isValidID("GE7IMTWIK5".toCharArray()));
    }  

Output -

false
true

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=341169&siteId=1