private boolean checkEsTin ( final String ptin ) { final char [ ] checkArray = { ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' } ; final char checkSum = ptin . charAt ( 8 ) ; final char calculatedCheckSum ; final int sum ; if ( StringUtils . isNumeric ( StringUtils . substring ( ptin , 0 , 8 ) ) ) { // dni sum = Integer . parseInt ( StringUtils . substring ( ptin , 0 , 8 ) ) % 23 ; calculatedCheckSum = checkArray [ sum ] ; } else if ( ptin . charAt ( 0 ) >= ' ' && ptin . charAt ( 0 ) <= ' ' ) { // cif sum = Integer . parseInt ( ptin . charAt ( 0 ) - ' ' + StringUtils . substring ( ptin , 1 , 8 ) ) % 23 ; calculatedCheckSum = checkArray [ sum ] ; } else { // nie final char letter = ptin . charAt ( 0 ) ; final String number = StringUtils . substring ( ptin , 1 , 8 ) ; int evenSum = 0 ; int oddSum = 0 ; for ( int i = 0 ; i < number . length ( ) ; i ++ ) { int charAsNum = number . charAt ( i ) - ' ' ; // Odd positions (Even index equals to odd position. i=0 equals first position) if ( i % 2 == 0 ) { // Odd positions are multiplied first. charAsNum *= 2 ; // If the multiplication is bigger than 10 we need to adjust oddSum += charAsNum < 10 ? charAsNum : charAsNum - 9 ; // Even positions // Just sum them } else { evenSum += charAsNum ; } } final int control_digit = 10 - ( evenSum + oddSum ) % 10 ; final char control_letter = "JABCDEFGHI" . charAt ( control_digit ) ; switch ( letter ) { case ' ' : case ' ' : case ' ' : case ' ' : // Control must be a digit calculatedCheckSum = ( char ) ( control_digit + ' ' ) ; break ; case ' ' : case ' ' : case ' ' : case ' ' : // Control must be a letter calculatedCheckSum = control_letter ; break ; default : // Can be either if ( control_letter == checkSum ) { calculatedCheckSum = control_letter ; } else { calculatedCheckSum = ( char ) ( control_digit + ' ' ) ; } break ; } } return checkSum == calculatedCheckSum ; }
check the Tax Identification Number number country version for Spain .