求解用java语言 编写一个身份证的类,可以用来验证身份证的长度,并可以显示地址码,生日,性别。

求解用java语言 编写一个身份证的类,可以用来验证身份证的长度,并可以显示地址码,生日,性别。

说明:公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码(最后一位如果为奇数,代表男生,偶数代表女生)和一位数字校验码。

验证码的规则:
1、将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 ;

2、将这17位数字和系数相乘的结果相加;

3、用加出来和除以11,看余数是多少?;

4、余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2;

5、通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的Ⅹ。如果余数是10,身份证的最后一位号码就是2;
最好可以解释一下每一步!!!!谢谢

第1个回答  2014-04-09
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入十八位身份证号码!");
String id=input.next();
if(id.length()!=18){
System.out.println("身份证号码长度不对!");
}
String top=id.substring(0, 17);
String sishu="7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2";
String[] xishu=sishu.split(",");
int sum=0;
for (int i = 0; i < top.length(); i++) {
int a=Integer.parseInt(top.charAt(i)+"");
int b=Integer.parseInt(xishu[i]);
sum+=a*b;
}
int xiaoyanjisuan=sum%11;
int xiaoyan=Integer.parseInt(id.substring(17, 18));
if(xiaoyanjisuan==xiaoyan){
System.out.println("身份证号码检测正确!");
}else{
System.out.println("身份证号码检测错误!");
return;
}
String dizhi=id.substring(0,6);
String nian=id.substring(6,10);
String yue=id.substring(10, 12);
String ri=id.substring(12,14);
int sex=Integer.parseInt(id.substring(16,17));
System.out.println("地址码为:"+dizhi);
System.out.println("生日为:"+nian+"年"+yue+"月"+ri+"日");
if(sex%2==0){
System.out.println("性别为:女");
}else{
System.out.println("性别为:男");
}
}追问

谢谢!

追答

上面的那个回答没有考虑到x 请加上一个判断!

追问

可以,很不错

追答

int xiaoyan=Integer.parseInt(id.substring(17, 18));
if(xiaoyanjisuan==xiaoyan){
System.out.println("身份证号码检测正确!");
}else{
System.out.println("身份证号码检测错误!");
return;
}
请将上面的代码替换为下面的代码

String xiaoyans="1,0,X,9,8,7,6,5,4,3,2";
String [] xiaoyanshuzu=xiaoyans.split(",");
String getxiaoyan=xiaoyanshuzu[xiaoyanjisuan];
String xiaoyan=id.substring(17, 18);
if(getxiaoyan.equals(xiaoyan)){
System.out.println("身份证号码检测正确!");
}else{
System.out.println("身份证号码检测错误!");
return;
}

第2个回答  2014-04-09
public class ID {

String[] lastId = {"1","0","X","9","8","7","6","5","4","3","2"};
int[] coefficient={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int newNum=0;
char sex = '男';
static byte b = 'a';
void CheckID(String id){
if(id.length()!=18){
System.out.println("无效的身份证1");
return;
}
for(int i = 0;i<17;i++){
int nub =  Integer.parseInt(id.substring(i, i+1))*coefficient[i];//将前面的身份证号码17位数分别乘以不同的系数
newNum+=nub;//将这17位数字和系数相乘的结果相加
}
if(id.substring(17, 18).equals(lastId[newNum%11])){
System.out.println("证件有效");
System.out.println("地址码:"+id.substring(0, 6));
System.out.println("生日:"+id.substring(6, 14));
sex = Integer.parseInt(id.substring(16, 17))%2==0?'女':'男'; //最后一位如果为奇数,代表男生,偶数代表女生
System.out.println("性别:"+sex);
}else{
System.out.println("无效的身份证2");
return;
}

}



public static void main(String[] args) {
// TODO Auto-generated method stub
ID id = new ID();
id.CheckID("421083199101285617");


}

}

//这下对了

第3个回答  2014-04-09
package com.tarena;

@SuppressWarnings("unchecked")
public static String IDCardValidate(String IDStr) throws ParseException {
String errorInfo = "";// 记录错误信息
String[] ValCodeArr = { "1", "0", "x", "9", "8", "7", "6", "5", "4",
"3", "2" };
String[] Wi = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7",
"9", "10", "5", "8", "4", "2" };
String Ai = "";
// ================ 号码的长度 15位或18位 ================
if (IDStr.length() != 15 && IDStr.length() != 18) {
errorInfo = "身份证号码长度应该为15位或18位。";
return errorInfo;
}
// =======================(end)========================
// ================ 数字 除最后以为都为数字 ================
if (IDStr.length() == 18) {
Ai = IDStr.substring(0, 17);
} else if (IDStr.length() == 15) {
Ai = IDStr.substring(0, 6) + "19" + IDStr.substring(6, 15);
}
if (isNumeric(Ai) == false) {
errorInfo = "身份证15位号码都应为数字 ; 18位号码除最后一位外,都应为数字。";
return errorInfo;
}
// =======================(end)========================
// ================ 出生年月是否有效 ================
String strYear = Ai.substring(6, 10);// 年份
String strMonth = Ai.substring(10, 12);// 月份
String strDay = Ai.substring(12, 14);// 月份
if (isDate(strYear + "-" + strMonth + "-" + strDay) == false) {
errorInfo = "身份证生日无效。";
return errorInfo;
}
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150
|| (gc.getTime().getTime() - s.parse(
strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
errorInfo = "身份证生日不在有效范围。";
return errorInfo;
}
if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
errorInfo = "身份证月份无效";
return errorInfo;
}
if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
errorInfo = "身份证日期无效";
return errorInfo;
}
// =====================(end)=====================
// ================ 地区码时候有效 ================
Hashtable h = GetAreaCode();
if (h.get(Ai.substring(0, 2)) == null) {
errorInfo = "身份证地区编码错误。";
return errorInfo;
}
// ==============================================
// ================ 判断最后一位的值 ================
int TotalmulAiWi = 0;
for (int i = 0; i < 17; i++) {
TotalmulAiWi = TotalmulAiWi
+ Integer.parseInt(String.valueOf(Ai.charAt(i)))
* Integer.parseInt(Wi[i]);
}
int modValue = TotalmulAiWi % 11;
String strVerifyCode = ValCodeArr[modValue];
Ai = Ai + strVerifyCode;
if (IDStr.length() == 18) {
if (Ai.equals(IDStr) == false) {
errorInfo = "身份证无效,不是合法的身份证号码";
return errorInfo;
}
} else {
return "";
}
// =====================(end)=====================
return "";
}

@SuppressWarnings("unchecked")
private static Hashtable GetAreaCode() {
Hashtable hashtable = new Hashtable();
hashtable.put("11", "北京");
hashtable.put("12", "天津");
hashtable.put("13", "河北");
hashtable.put("14", "山西");
hashtable.put("15", "内蒙古");
hashtable.put("21", "辽宁");
hashtable.put("22", "吉林");
hashtable.put("23", "黑龙江");
hashtable.put("31", "上海");
hashtable.put("32", "江苏");
hashtable.put("33", "浙江");
hashtable.put("34", "安徽");
hashtable.put("35", "福建");
hashtable.put("36", "江西");
hashtable.put("37", "山东");
hashtable.put("41", "河南");
hashtable.put("42", "湖北");
hashtable.put("43", "湖南");
hashtable.put("44", "广东");
hashtable.put("45", "广西");
hashtable.put("46", "海南");
hashtable.put("50", "重庆");
hashtable.put("51", "四川");
hashtable.put("52", "贵州");
hashtable.put("53", "云南");
hashtable.put("54", "西藏");
hashtable.put("61", "陕西");
hashtable.put("62", "甘肃");
hashtable.put("63", "青海");
hashtable.put("64", "宁夏");
hashtable.put("65", "新疆");
hashtable.put("71", "台湾");
hashtable.put("81", "香港");
hashtable.put("82", "澳门");
hashtable.put("91", "国外");
return hashtable;
}

private static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (isNum.matches()) {
return true;
} else {
return false;
}
}

public static boolean isDate(String strDate) {
Pattern pattern = Pattern
.compile("^((//d{2}(([02468][048])|([13579][26]))[//-/////s]?((((0?[13578])|(1[02]))[//-/////s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[//-/////s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[//-/////s]?((0?[1-9])|([1-2][0-9])))))|(//d{2}(([02468][1235679])|([13579][01345789]))[//-/////s]?((((0?[13578])|(1[02]))[//-/////s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[//-/////s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[//-/////s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(//s(((0?[0-9])|([1-2][0-3]))//:([0-5]?[0-9])((//s)|(//:([0-5]?[0-9])))))?$");
Matcher m = pattern.matcher(strDate);
if (m.matches()) {
return true;
} else {
return false;
}
}本回答被网友采纳
相似回答