`

java判断身份证信息小程序(无图形界面)

阅读更多
请输入要查询的身份证号码,输入exit退出:
53010219200508011x
身份证号:53010219200508011x
所属地区:云南省
出生日期:1920年5月8日
性别:男性
校验位:正确。
------------------------------------------
请输入要查询的身份证号码,输入exit退出:
530102192005080111
身份证号:530102192005080111
所属地区:云南省
出生日期:1920年5月8日
性别:男性
校验位:错误,校验为应该是:x
------------------------------------------
请输入要查询的身份证号码,输入exit退出:

package identity_card;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 验证身份证
 */
public class Go {

	/** 保存全国各地区身份证省份代码 */
	private Map<Integer, String> city = new HashMap<Integer, String>();

	/** 主函数,不多说 */
	public static void main(String[] args) {
		new Go().init();
	}

	/** 初始化相关参数及数据 */
	public void init() {
		insert();
		input();
	}

	/** 接收输入,判定结果 */
	public void input() {
		Scanner sc = new Scanner(System.in);
		while (true) {
			System.out.println("请输入要查询的身份证号码,输入exit退出:");
			String str = sc.nextLine();// 接收一行输入
			if (str.toString().equals("exit")) {// 判断是否输入的是"exit",如果不是,继续执行,否则退出
				System.out.println("系统已经退出。");
				System.exit(0);// 退出
			}
			if (str.length() == 18) {// 身份证必须等于18位才可以判断,否则不判断,这里面判断等于18位的
				boolean status = true;// 设初始状态为可行的
				for (int i = 0; i < str.length() - 1; i++) {// 这里面只判断前17位是否是数字,因为最后一位可以是"x"或"X"
					if (!Character.isDigit(str.charAt(i))) {// 如果当前字节不是数字,打印提示,改变status的值
						System.out.println("你输入的内容不符合要求。");
						status = false;
					}
				}
				/*
				 * 上面的循环走完了,如果没有出错,status还是等于true,所以这里我们再判断最后一位
				 * 如果上面出错了,status不等于true,那么下面的if就不执行了
				 */
				if (status) {// 这个地方也可以写成if (status==true){
					/*
					 * 前17位判断完成后,接下来,我们判定最后一位,最后一位只能是"x"或"X"
					 */
					if (Character.isDigit(str.charAt(17))
							|| str.charAt(17) == 'x' || str.charAt(17) == 'X') {// 如果满足继续执行,否则执行else
						/** 能跑到这里面,说明前面的判断都没有问题,现在开始拆分内容了 */
						{// 这一组括号其实是没有用的,在这是为了区分模块,这个模块是显示输入的内容的
							System.out.print("身份证号:");
							System.out.println(str);
						}
						{// 这个模块是用来显示地区的
							String home = city.get(new Integer(str.substring(0,
									2)));// 查询所属城市,如果查不到为null
							System.out.print("所属地区:");
							if (home != null)
								System.out.println(home);
							else
								System.out.println("不存在");
						}
						{// 这个模块是用来显示出生日期的
							int year = Integer.valueOf(str.substring(6, 10));
							int month = Integer.valueOf(str.substring(10, 12));
							int day = Integer.valueOf(str.substring(12, 14));
							System.out.print("出生日期:");
							if (year > 0) {// 年份必须是大于0的
								if (month > 0 && month <= 12) {// 月必须大于0,月小于13,日还要单独算
									if (day > 0) {// 这里只判定大于0,小于多少,单独判定,因为年份月份不同,天数不同
										int days = getDays(year, month);// 判断当前年份下,当前月下有多少天,区分平闰年
										if (day <= days) {
											System.out.print(year + "年");
											System.out.print(month + "月");
											System.out.println(day + "日");
										} else {
											System.out.println("不存在,具体几日有问题。");
										}
									} else {
										System.out.println("不存在,具体几日有问题。");
									}
								} else {
									System.out.println("不存在,具体几月有问题。");
								}
							} else {
								System.out.println("不存在,具体哪一年有问题。");
							}
						}
						{// 这个模块是用来显示性别的
							System.out.print("性别:");
							System.out.println((Integer.valueOf(str.substring(
									16, 17))) % 2 == 1 ? "男性" : "女性");
						}
						{// 这个模块是用来显示校验码是否正确的
							int last = getCheck(str);// 存放最后一位计算出来的值,用于和输入的值进行比较
							System.out.print("校验位:");
							if ((str.substring(17, 18) + "").equals(String
									.valueOf(last))
									|| (str.substring(17).equals("x") && last == -1)
									|| (str.substring(17).equals("X") && last == -1)) {// 比较最后一位是否和计算的值一致
								System.out.println("正确。");
							} else {
								System.out.println("错误,校验为应该是:"
										+ ((last != -1) ? last : "x"));
							}
						}
					} else {
						System.out.println("你输入的内容不符合要求。");
					}
				}
			} else {// 这里面判断不等于18位的
				System.out.println("你输入的内容不符合要求。");
			}
			System.out.println("------------------------------------------");
		}
	}

	/** 判断当前年份下,当前月下有多少天,区分平闰年 */
	public int getDays(int year, int month) {// 判断当前年份下,当前月下有多少天,区分平闰年
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {// 这些月下,不管是平年,还是闰年,都是31天
			return 31;
		} else if (month == 4 || month == 6 || month == 9 || month == 11) {// 这些月下,不管是平年,还是闰年,都是30天
			return 30;
		} else {
			if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {// 这是闰年
				return 29;
			} else { // 这是平年
				return 28;
			}
		}
	}

	/** 获取身份证校验码,参数是身份证字符串,返回值-1代表的是"x"或"X" */
	public int getCheck(String str) {
		/** 这个变量用来存放所有校验和 */
		int sum = 0;
		/** 这个数组里面存放的是身份证校验位系数,coefficient的缩写 */
		int[] coe = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
		/** 这个数组里面存放的是身份证校验对应的最后一位身份证号码 */
		int[] check = { 1, 0, -1, 9, 8, 7, 6, 5, 4, 3, 2 };// -1代表的是"x"或"X"
		for (int i = 0; i < str.length() - 1; i++) {
			sum = sum + Integer.parseInt(str.charAt(i) + "") * coe[i];
		}
		return check[sum % 11];
	}

	/** 将全国各地区身份证省份代码插入到private Map<Integer, String> city集合中 */
	public void insert() {
		city.put(11, "北京市");
		city.put(12, "天津市");
		city.put(13, "河北省");
		city.put(14, "山西省");
		city.put(15, "内蒙古自治区");
		city.put(21, "辽宁省");
		city.put(22, "吉林省");
		city.put(23, "黑龙江省");
		city.put(31, "上海市");
		city.put(32, "江苏省");
		city.put(33, "浙江省");
		city.put(34, "安徽省");
		city.put(35, "福建省");
		city.put(36, "江西省");
		city.put(37, "山东省");
		city.put(41, "河南省");
		city.put(42, "湖北省");
		city.put(43, "湖南省");
		city.put(44, "广东省");
		city.put(45, "广西壮族自治区");
		city.put(46, "海南省");
		city.put(5, "0重庆市");
		city.put(51, "四川省");
		city.put(52, "贵州省");
		city.put(53, "云南省");
		city.put(54, "西藏自治区");
		city.put(61, "陕西省");
		city.put(62, "甘肃省");
		city.put(63, "青海省");
		city.put(64, "宁夏回族自治区");
		city.put(65, "新疆维吾尔自治区");
		city.put(71, "台湾省(886)");
		city.put(81, "香港特别行政区");
		city.put(82, "澳门特别行政区");
	}
}
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics