public static void main(String[] args) {
String str = "34052419800101001X";
print(IDCardNoVerify(str));
}
public static boolean IDCardNoVerify(String IDCardNo) {
if (!IDCardNoFormatVerify(IDCardNo)) {
return false;
}
int[] coef = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
int sum = 0;
for (int i = 0; i < 17; i++) {
sum += Integer.parseInt(IDCardNo.substring(i, i + 1)) * coef[i];
}
int remainder = sum % 11;
Integer lastNum = ((11 - remainder) + 1) % 11;
String last = null;
if (lastNum == 10) {
last = "X";
}
else {
last = lastNum.toString();
}
if (IDCardNo.endsWith(last)) {
return true;
}
else {
return false;
}
}
public static boolean IDCardNoFormatVerify(String IDCardNo) {
String regexp = "^[0-9]{17}[0-9xX]$";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(IDCardNo);
return matcher.matches();
}`