重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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 }
创新互联建站成立与2013年,是专业互联网技术服务公司,拥有项目成都网站设计、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元中阳做网站,已为上家服务,为中阳各地企业和个人服务,联系电话:18982081108
/**
* 定义一个方法,输入一个身份证号码(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;
}
}
如果只要判断有非法的字符(除0-9和Xx外)可用正则表达式publicstaticvoidmain(String[]args){//TODOcodeapplicationlogichereStrings="2142213weqrwe32";StringregEx="[^0-9Xx]";Patternpat=Pattern点抗 pile(regEx);Matchermat=pat.matcher(s);booleanrs=mat.find();if(rs){System.out.print("有非法字符");}另外,校验身份证号码有专门程序的,可直接校验身份证号是否正确,在自己在网上找下
package com.yanlun.starter.utils;
import java.util.regex.Pattern;
/**
* @author 作者:Yan,Email:yanlun0323@163点抗
* @version 创建时间:2017年5月26日 上午10:42:09
*/
public class Validation {
public static final Pattern REX_DATE_YYYYMM_PATTERN = Pattern点抗 pile("^[0-9]{6}$");
public static final Pattern REX_DATE_YYYYMMDD_PATTERN = Pattern点抗 pile("^[0-9]{8}$");
// 身份证校验加权因子
public static final Integer[] ID_NUM_FACTOR = new Integer[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,
1 };
// 身份证第18位校验码
public static final String[] ID_NUM_PARITY_BIT = new String[] { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3",
"2" };
/*
* 是否空字符串
*
* @param c 文本或其它基本数字类型对象
*/
public static boolean isEmpty(Object c) {
return c == null || c.toString().trim().equals("");
}
/**
* 判断是否为“”式的时期
*
* @param dateStr
* @return
*/
private static boolean isDate6(String dateStr) {
if (isEmpty(dateStr) || !REX_DATE_YYYYMM_PATTERN.matcher(dateStr).matches()) {
return false;
}
return isValidDateRange(date6Split(dateStr));
}
/**
* 判断是否为“YYYYMMDD”式的时期
*
* @param dateStr
* @return
*/
private static boolean isDate8(String dateStr) {
if (isEmpty(dateStr) || !REX_DATE_YYYYMMDD_PATTERN.matcher(dateStr).matches()) {
return false;
}
return isValidDateRange(date8Split(dateStr));
}
private static boolean isLeapYear(Integer year) {
return ((year % 4 == 0) (year % 100 != 0)) || (year % 400 == 0);
}
private static boolean isInvalidYear(Integer year) {
return year 1700 || year 2500;
}
private static boolean isInvalidMonth(Integer month) {
return month 1 || month 12;
}
private static boolean isInvalidDay(Integer day, Integer month, Integer year) {
Integer[] iaMonthDays = new Integer[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeapYear(year))
iaMonthDays[1] = 29;
return day 1 || day iaMonthDays[month - 1];
}
/**
* split date 0-YY,1-MM,2-DD
*
* @param dateStr
* @return
*/
private static Integer[] date6Split(String dateStr) {
final Integer YEAR_BASE = 1900;
Integer year = null, month = null, day = null;
year = YEAR_BASE + Integer.valueOf(dateStr.substring(0, 2));
month = Integer.valueOf(dateStr.substring(2, 4));
day = Integer.valueOf(dateStr.substring(4, 6));
return new Integer[] { year, month, day };
}
/**
* split date 0-YYYY,1-MM,2-DD
*
* @param dateStr
* @return
*/
private static Integer[] date8Split(String dateStr) {
Integer year = null, month = null, day = null;
year = Integer.valueOf(dateStr.substring(0, 4));
month = Integer.valueOf(dateStr.substring(4, 6));
if (dateStr.length() == 8) {
day = Integer.valueOf(dateStr.substring(6, 8));
return new Integer[] { year, month, day };
} else {
return new Integer[] { year, month };
}
}
private static boolean isValidDateRange(Integer[] dateSplitResult) {
Integer year = dateSplitResult[0], month = dateSplitResult[1], day = dateSplitResult[2];
if (isInvalidYear(year))
return false;
if (isInvalidMonth(month))
return false;
if (isInvalidDay(day, month, year))
return false;
return true;
}
/**
* 18位/15位身份证号码校验
*
* @param idNumber
* @return
*/
public static boolean isIdentityCardNum(String idNumber) {
if (isEmpty(idNumber) || (idNumber.length() != 18 idNumber.length() != 15)) {
return false;
}
// initialize
if (idNumber.length() == 18) {
// check date
String date8 = idNumber.substring(6, 14);
if (isDate8(date8) == false) {
return false;
}
int totalMulAiWi = 0;
char charAt;
// check and set value, calculate the totalmulAiWi
for (int i = 0; i 17; i++) {
charAt = idNumber.charAt(i);
if (charAt '0' || charAt '9') {
return false;
}
totalMulAiWi += Integer.valueOf(String.valueOf(charAt)) * ID_NUM_FACTOR[i];
}
// calculate the check digit
String checkDigit = ID_NUM_PARITY_BIT[totalMulAiWi % 11];
// check last digit
if (!checkDigit.equalsIgnoreCase(String.valueOf(idNumber.charAt(17)))) {
return false;
}
} else {// length is 15
// check date
String date6 = idNumber.substring(6, 12);
if (isDate6(date6) == false) {
return false;
}
}
return true;
}
}