身份证号码的升位和验证
2013年12月13日
⁄ 综合
⁄ 共 7494字 ⁄ 字号
小 中 大
- import java.io.BufferedReader;
-
- import java.io.InputStreamReader;
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class IdCard {
-
-
-
-
-
- static int[] wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
-
-
-
- static char[] ai = { '1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2' };
-
-
-
- public static void main(String args[]) {
-
-
-
-
-
- try {
-
- boolean flag = false;
-
- while (flag) {
-
- BufferedReader reader = new BufferedReader(
-
- new InputStreamReader(System.in));
-
- System.out.println("15位身份证号:");
-
- String lowerid = reader.readLine();
-
- if (lowerid.equals("quit")) {
-
- System.out.println("bye~~");
-
- break;
-
- }
-
- System.out.println("8位出生日期(19791216):");
-
- String birth = reader.readLine();
-
-
-
- System.out.println("请输入性别");
-
- String sex = reader.readLine();
-
-
-
- System.out.println(checkIdNumber(lowerid, birth, sex));
-
- System.out.println("18位号:" + upperIdNumber(lowerid, birth));
-
- System.out.println(checkIdNumber(upperIdNumber(lowerid, birth),
-
- birth, sex));
-
- }
-
- } catch (Exception e) {
-
- System.out.println(e);
-
- }
-
-
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static String upperIdNumber(String lowerId, String birthday) {
-
- if (lowerId.length() != 15) {
-
- return "请录入15位身份证号码。";
-
- } else {
-
- return lowerId.substring(0, 6) + birthday.substring(0, 2)
-
- + lowerId.substring(6) + ai[checkBit(lowerId, birthday)];
-
- }
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static int checkBit(String lowerId, String birthday) {
-
- if (lowerId.length() != 15)
-
- return -1;
-
-
-
- lowerId = lowerId.substring(0, 6) + birthday.substring(0, 2)
-
- + lowerId.substring(6);
-
- int sum = 0;
-
-
-
- for (int i = 1; i < lowerId.length() + 1; i++) {
-
- sum = sum + wi[i - 1]
-
- * (Integer.parseInt(lowerId.substring(i - 1, i)));
-
- }
-
-
-
-
-
- int mod = sum % 11;
-
-
-
- return mod;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static int checkBit(String id) {
-
- String lowerId = id.substring(0, 17);
-
- int sum = 0;
-
- for (int i = 1; i < lowerId.length() + 1; i++) {
-
- sum = sum + wi[i - 1]
-
- * (Integer.parseInt(lowerId.substring(i - 1, i)));
-
- }
-
-
-
- int mod = sum % 11;
-
-
-
- return mod;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static String checkIdNumber(String id, String birthday, String sex) {
-
- String result = "";
-
- int len = id.length();
-
- if (len == 15) {
-
-
-
- result = checkId_15(id, birthday, sex);
-
-
-
- } else if (len == 18) {
-
-
-
- result = checkId_18(id, birthday, sex);
-
-
-
- } else {
-
- result = "false|身份证号长度错误,只能是15位或18位。";
-
- }
-
-
-
- return result;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static String checkId_15(String id, String birthday, String sex) {
-
- String result = "";
-
- String birth_id = id.substring(6, 12);
-
- String birth = birthday.substring(2);
-
- if (birth_id.equals(birth)) {
-
-
-
- if (sex.equals("男")) {
-
-
-
- String temp = id.substring(14);
-
- int isex = Integer.parseInt(temp);
-
- if (isex % 2 == 1) {
-
- result = "true|身份证号通过验证。";
-
- } else {
-
- result = "false|身份证号与性别不相符。";
-
- }
-
- } else if (sex.equals("女")) {
-
-
-
- String temp = id.substring(14);
-
- int isex = Integer.parseInt(temp);
-
- if (isex % 2 == 0) {
-
- result = "true|身份证号通过验证。";
-
- } else {
-
- result = "false|身份证号与性别不相符。";
-
- }
-
- } else {
-
- result = "false|性别录入错误。";
-
- }
-
- } else {
-
-
-
- result = "false|出生日期与身份证中的出生日期不相符。";
-
- }
-
-
-
- return result;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static String checkId_18(String id, String birthday, String sex) {
-
- String result = "";
-
- String birth_id = id.substring(6, 14);
-
- if (birth_id.equals(birthday)) {
-
-
-
- if (sex.equals("男")) {
-
-
-
- String temp = id.substring(16, 17);
-
-
-
- int isex = Integer.parseInt(temp);
-
- if (isex % 2 == 1) {
-
- result = "true|身份证号通过验证。";
-
- } else {
-
- result = "false|身份证号与性别不相符。";
-
- }
-
- } else if (sex.equals("女")) {
-
-
-
- String temp = id.substring(16, 17);
-
- int isex = Integer.parseInt(temp);
-
- if (isex % 2 == 0) {
-
- result = "true|身份证号通过验证。";
-
- } else {
-
- result = "false|身份证号与性别不相符。";
-
- }
-
- } else {
-
- result = "false|性别录入错误。";
-
- }
-
- } else {
-
-
-
- result = "false|出生日期与身份证中的出生日期不相符。";
-
- }
-
-
-
- return result;
-
- }
-
- }