/** * * @param code * @return 如果返回值是一个大于0的整数 如果返回-2表示所传的参数为空 ,如果返回值是-1 表示字符串中含有非数字字符 */ public static int getCheckCode(String code){ int checkCode = -1; if(code == null || "".equals(code)){ return -2; }else if(!Pattern.compile("^[0-9]*$").matcher(code).matches()){ checkCode = -1; }else{ try{ int evensum = 0; //偶数位的和 int oddsum = 0; //奇数位的和 evensum += Integer.parseInt(code.substring(11,12)); evensum += Integer.parseInt(code.substring(9,10)); evensum += Integer.parseInt(code.substring(7,8)); evensum += Integer.parseInt(code.substring(5,6)); evensum += Integer.parseInt(code.substring(3,4)); evensum += Integer.parseInt(code.substring(1,2)); evensum *= 3; oddsum += Integer.parseInt(code.substring(10,11)); oddsum += Integer.parseInt(code.substring(8,9)); oddsum += Integer.parseInt(code.substring(6,7)); oddsum += Integer.parseInt(code.substring(4,5)); oddsum += Integer.parseInt(code.substring(2,3)); oddsum += Integer.parseInt(code.substring(0,1)); int sum = evensum + oddsum; int ck = 0; if(sum % 10 == 0){ ck = sum; }else{ ck = (sum/10+1)*10; } checkCode = ck-sum; }catch(NumberFormatException e){ System.out.println("编码的格式不正确!"); }catch(Exception e){ e.printStackTrace(); } } return checkCode; }