十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
首先判断位数,就用leanth 吧。判断 出生 这个可以先 取 字串,在判断! 判断是不是数字,就用正者表达式撒,最后一位的判断还是用substing吧
成都创新互联是由多位在大型网络公司、广告设计公司的优秀设计人员和策划人员组成的一个具有丰富经验的团队,其中包括网站策划、网页美工、网站程序员、网页设计师、平面广告设计师、网络营销人员及形象策划。承接:做网站、网站建设、网站改版、网页设计制作、网站建设与维护、网络推广、数据库开发,以高性价比制作企业网站、行业门户平台等全方位的服务。
/**
* 定义一个方法,输入一个身份证号码(18)位,最终返回一个字符串,格式如下:“尊敬的男士(女士),您是****年**月**日出生的。”
* 要求:如果输入的身份证不是18位,或者格式不正确(包含非数字字符,除了最后一位的X除外)则返回一个字符串“您输入的身份证号码有误”
* */
import java.util.Scanner;
public class JudgeID {
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.println("请输入您的id号码");
String id = reader.next();
System.out.println(judgeID(id));
}
public static String judgeID(String IDNumber){
int len = IDNumber.length();
String wrongMsg = "您输入的身份证号有误";
String correctMsg = null;
char lastChar = IDNumber.charAt(len-1);
String year = null;
String month = null;
String day = null;
if(len != 18){
return wrongMsg;
}
for(int i = 0; i len-1; i++){
if(IDNumber.charAt(i) '0' || IDNumber.charAt(i) '9' ){
return wrongMsg;
}
}
if(lastChar != 'X' lastChar '0' lastChar '9'){
return wrongMsg;
}
//如果身份证格式是没有问题的,那个打印年月日信息
year = IDNumber.substring(6,10);
month = IDNumber.substring(10,12);
day = IDNumber.substring(12,14);
correctMsg = "您是"+year+"年"+month+"月"+day+"日出生!";
return correctMsg;
}
}
使用java语言实现一个身份证号码转换的程序:把15位号码转换为18位的号码功能要求:1.判断输入身份证号位数是否正确,判断输入是否有效(如输入非数字字符),否则提示用户重新输入。
2.计算身份证号码。
3.根据身份证号的第7-14位(YYYYMMDD)显示出生日期。4.根据身份证号的第17位(奇数为男性,偶数为女性),显示性别。
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();
}
}
public static void getString(){\x0d\x0a String a,b;\x0d\x0a while (true) {\x0d\x0a Scanner input = new Scanner(System.in);\x0d\x0a System.out.println("请输入您的18位身份证号码!!");\x0d\x0a a = (String) input.next();\x0d\x0a if (a.length() == 18) {\x0d\x0a a.matches("/D");\x0d\x0a b = a.substring(6, 14);\x0d\x0a System.out.println("请的出生日期为:" + b);\x0d\x0a if(a.charAt(16)%2 == 0)\x0d\x0a {\x0d\x0a System.out.println("您的性别是女性!");\x0d\x0a }else{\x0d\x0a System.out.println("您的性别是男性!");\x0d\x0a }\x0d\x0a } else {\x0d\x0a System.out.println("您输入的身份证错误!!");\x0d\x0a }\x0d\x0a }\x0d\x0a }
如果只要判断有非法的字符(除0-9和Xx外)可用正则表达式publicstaticvoidmain(String[]args){//TODOcodeapplicationlogichereStrings="2142213weqrwe32";StringregEx="[^0-9Xx]";Patternpat=Pattern.compile(regEx);Matchermat=pat.matcher(s);booleanrs=mat.find();if(rs){System.out.print("有非法字符");}另外,校验身份证号码有专门程序的,可直接校验身份证号是否正确,在自己在网上找下