⑴ 使用java判斷輸入身份證號位數是否正確判斷輸入是否有效

public static void getString(){
String a,b;
while (true) {
Scanner input = new Scanner(System.in);
System.out.println("請輸入您的18位身份證號碼!!");
a = (String) input.next();
if (a.length() == 18) {
a.matches("/D");
b = a.substring(6, 14);
System.out.println("請的出生日期為:" + b);
if(a.charAt(16)%2 == 0)
{
System.out.println("您的性別是女性!");
}else{
System.out.println("您的性別是男性!");
}
} else {
System.out.println("您輸入的身份證錯誤!!");
}
}
}

⑵ java身份證號位數校驗

function checkIdcard(num)
{
num = num.toUpperCase();
//身份證號碼為15位或者18位,位時全為數字,18位前17位為數字,最後一位是校驗位,可能為數字或字元X。
if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num)))
{
//alert('輸入的身份證號長度不對,或者號碼不符合規定!\n15位號碼應全為數字,18位號碼末位可以為數字或X。');
return false;
}
//校驗位按照ISO 7064:1983.MOD 11-2的規定生成,X可以認為是數字10。
//下面分別分析出生日期和校驗位
var len, re;
len = num.length;
if (len == 15)
{
re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
var arrSplit = num.match(re);

//檢查生日日期是否正確
var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]);
var bGoodDay;
bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
if (!bGoodDay)
{
//alert('輸入的身份證號里出生日期不對!');
return false;
}
else
{
//將15位身份證轉成18位
//校驗位按照ISO 7064:1983.MOD 11-2的規定生成,X可以認為是數字10。
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var nTemp = 0, i;
num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6);
for(i = 0; i < 17; i ++)
{
nTemp += num.substr(i, 1) * arrInt[i];
}
num += arrCh[nTemp % 11];
return true;
}
}
if (len == 18)
{
re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
var arrSplit = num.match(re);

//檢查生日日期是否正確
var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
var bGoodDay;
bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
if (!bGoodDay)
{
//alert(dtmBirth.getYear());
//alert(arrSplit[2]);
//alert('輸入的身份證號里出生日期不對!');
return false;
}
else
{
//檢驗18位身份證的校驗碼是否正確。
//校驗位按照ISO 7064:1983.MOD 11-2的規定生成,X可以認為是數字10。
var valnum;
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var nTemp = 0, i;
for(i = 0; i < 17; i ++)
{
nTemp += num.substr(i, 1) * arrInt[i];
}
valnum = arrCh[nTemp % 11];
if (valnum != num.substr(17, 1))
{
//alert('18位身份證的校驗碼不正確!應該為:' + valnum);
return false;
}
return true;
}
}
return false;
}
var result = checkIdcard(sfzh);
if (!result)
{
$.messager.alert('提示信息','身份證號碼格式不正確');
return;
}

⑶ Java-18位身份證號的正則表達式。幫忙拆一下代碼

|^ 匹配字元串的開始
[1-9] 第1位:1-9
\d{5} 第版2-6位:0-9
[1-9] 第7位:1-9
\d{3} 第8-10位:0-9
((0\d)|(1[0-2])):
第11-12位:(00-09)權或(10-12)
(([0|1|2]\d)|3[0-1])
第13-14位:(00-29)或(30-31)
\d{3} 第15-17位:0-9
([0-9]|X)
第18位:0-9或X
$ 匹配字元串的結束

⑷ 求驗證身份證號碼的java程序!

import java.io.*;

public class ID{
private String temp ;
private final char[] cc = {1,0,'X',9,8,7,6,5,4,3,2};
public String getID(){
try{
System.out.println("Input ID,please [input]");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
temp = br.readLine();
}
catch(IOException e){}
System.out.println(" make sure of it:"+temp);
return temp;
}

public void checkit(){
String temp1 = temp;
if(temp1.length() == 18){
System.out.println(temp1.substring(6,10)+"year "+
temp1.substring(10,12)+"month "+
temp1.substring(12,14)+"day "+
"sex: "+checkSex(temp1.charAt(16)) );
}
else if(temp1.length() == 15){
String id = makeit(temp1);
System.out.println(id.substring(6,10)+"year "+
id.substring(10,12)+"month "+
id.subSequence(12,14)+"day "+
"sex: "+ checkSex(id.charAt(16)));
}
else
System.out.println("your input is Error,please input again!");
}

public String checkSex(char c){
if(c%2 == 0)
return "girl";
else
return "boy";
}

public int LastNum(String temp3){
int index = 0;
for(int i = 16;i>= 0;i--){
double wi = Math.pow(2,(i-1)) %11;
int ai = (int)temp3.charAt(i);
index += ai*wi;
}
index = index % 11;

index = (int)this.cc[index];
return index;
}

public String makeit(String temp2){
StringBuffer temp3 = new StringBuffer(temp2);
temp3.insert(6,"19");
temp3.insert(17,LastNum(temp3.toString()));
return temp3.toString();
}

public static void main(String[] args){
ID id = new ID();
id.temp = id.getID();
id.checkit();
}

}

import java.io.*;

public class ID{
private String temp ;
private final char[] cc = {1,0,'X',9,8,7,6,5,4,3,2};
public String getID(){
try{
System.out.println("Input ID,please [input]");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
temp = br.readLine();
}
catch(IOException e){}
System.out.println(" make sure of it:"+temp);
return temp;
}

public void checkit(){
String temp1 = temp;
if(temp1.length() == 18){
System.out.println(temp1.substring(6,10)+"year "+
temp1.substring(10,12)+"month "+
temp1.substring(12,14)+"day "+
"sex: "+checkSex(temp1.charAt(16)) );
}
else if(temp1.length() == 15){
String id = makeit(temp1);
System.out.println(id.substring(6,10)+"year "+
id.substring(10,12)+"month "+
id.subSequence(12,14)+"day "+
"sex: "+ checkSex(id.charAt(16)));
}
else
System.out.println("your input is Error,please input again!");
}

public String checkSex(char c){
if(c%2 == 0)
return "girl";
else
return "boy";
}

public int LastNum(String temp3){
int index = 0;
for(int i = 16;i>= 0;i--){
double wi = Math.pow(2,(i-1)) %11;
int ai = (int)temp3.charAt(i);
index += ai*wi;
}
index = index % 11;

index = (int)this.cc[index];
return index;
}

public String makeit(String temp2){
StringBuffer temp3 = new StringBuffer(temp2);
temp3.insert(6,"19");
temp3.insert(17,LastNum(temp3.toString()));
return temp3.toString();
}

public static void main(String[] args){
ID id = new ID();
id.temp = id.getID();
id.checkit();
}

}

⑸ 用JAVA來判斷你所輸入的18位身份證號碼的性別

其實只要看倒數第二位的數字,是奇數還是偶數。奇數是男,偶數是女。也就是原先15位身份證號的最後一位,性別區分就看這位數。

⑹ 身份證號在java中的聲明類型用什麼

string因為身份證有的後頭是帶字母的
比如我就見過帶x結尾的身份證號碼。

⑺ java怎樣解析帶有xxxxxxxx的身份證號

Stringstr="320324xxxxxxxx4172";
Stringreg="^.{6}(.+).{4}$";
str=str.replaceAll(reg,"$1");

⑻ java中如何從鍵盤輸入一個18位的身份證號碼保存到字元數組Card中

System.out.println("請輸入您的身份證:");
Scanner scanner = new Scanner(System.in);
String[] cards = new String[];
cards[0] = scanner.nextLine();
循環輸入並存入數組自己實現,很容易

⑼ java編程輸入18位身份證號辦別女性男女 代碼

18為身份證號碼的第17位數字表示性別:奇數表示男性,偶數表示女性

所有代碼中,只要截取出第17位的數字出來就可以判斷性別,如:

  • String sex;

  • // 判斷性別

  • if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {

  • sex = "女";

  • } else {

  • sex = "男";

  • }

⑽ JAVA從控制台輸入身份證號碼,把生日用星號代替再列印出來

用字元串截取啊、
比如ID=370304198411061433
370304:代表的是地區編號
19841106:出生日期
1433:個人區分號
生日 str = ID.substring(9,13); 這樣截取後的子字元串str就是1106。
最後再將截取後的字元串替換成星號。str="****";
最後輸出就是:ID.substring(1,9)+str+ID.substring(9,17)
當然 這樣比較麻煩 可以試試stringBuff類。