转换成int不可能,因为超出范围
但是要判断很简单的面试题这么简单吗?
用正则表达式吧。

解决方案 »

  1.   

    楼上的朋友,当然不是整个字符串转成int,而是截取字符串的每个字符再转成int
      public boolean validate(String CT){
        String temp = CT.trim();
        if(temp=="" || temp==null)return false;    if(temp.length()>=15)return false;//判断长度不能超过15
        
        //以下是判断字符串合法性,对最后一个数是可以是字符类型
        String str1 = temp.substring(0,temp.length()-1);
        String str2 = temp.substring(temp.length()-1);
        Matcher matcher = Pattern.compile("[0-9]").matcher(str1);
        if(!matcher.matches())return false;
        matcher = Pattern.compile("[0-9A-Za-z]").matcher(str2);
        if(!matcher.matches())return false;
       
        //转换为int
        int[] result = new int[temp.length()-1];
        for(int i=0;i<temp.length()-1;i++){
          result[i] = (int)temp.charAt(i);      //对8到9的位置进行判断是不是生日(数字)
          //这里楼主意思没表达清楚,如果只是判断是否是数字,那上面的正则表达式就行了
         
        }
        return true;
      }大概是这样吧,楼主再改改就OK了
      

  2.   

    if(temp=="" || temp==null)return false;
    朋友在句这样写!
        if(temp==null || temp.equals(""))return false;
     先知道有没有!才可以判断是不是“”!
      

  3.   

    //对8到9的位置进行判断是不是生日(数字)
    取第8和第9位判断是不是在01到31之间,
    public boolean shengri(String str){
    String temp=str.substring(7,9);//下标从0开始,取第8个到第10个,包括第8个,但是不包括第10个
    try{
        if(1<Integer.parseInt(temp)&&Integer.parseInt(temp)<31)
    return true;
    }
    catch(Exception e){
    System.out.println("对不起,您的身份证非法");
    return false;
    }
    }
      

  4.   

    js实现的 放到 html中就能实现 看一看算法就行了
      //身份证的验证   
      function isIdCardNo(num) 
          { 
            if (isNaN(num)) {alert("输入的不是数字!"); return false;} 
            var len = num.length, re;  
            if (len == 15) 
              re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/); 
            else if (len == 18) 
              re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/); 
            else {alert("输入的数字位数不对!"); return false;} 
            var a = num.match(re); 
            if (a != null) 
            { 
              if (len==15) 
              { 
                var D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]); 
                var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&
    D.getDate()==a[5]; 
              } 
              else 
              { 
                var D = new Date(a[3]+"/"+a[4]+"/"+a[5]); 
                var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&
    D.getDate()==a[5]; 
              } 
              if (!B) {alert("输入的身份证号 "+ a[0] +" 里出生日期不对!"); return false;} 
            } 
            return true; 
          } 
      
      
    function showBirthday(val){var birthdayValue;
    if(15==val.length){//15位身份证号码
    birthdayValue = val.charAt(6)+val.charAt(7);
    if(parseInt(birthdayValue)<10){
    birthdayValue = '20'+birthdayValue;
    }else{
    birthdayValue = '19'+birthdayValue;
    }
    birthdayValue=birthdayValue+'-'+val.charAt(8)+val.charAt(9)+'-'+val.charAt(10)+val.charAt(11);
    if(parseInt(val.charAt(14)/2)*2!=val.charAt(14))
    document.all.sex.value='男';
    else
    document.all.sex.value='女';
    document.all.birthday.value=birthdayValue;
    }
    if(18==val.length){//18位身份证号码
    birthdayValue=val.charAt(6)+val.charAt(7)+val.charAt(8)+val.charAt(9)+'-'+val.charAt(10)+val.charAt(11)+'-'+val.charAt(12)+val.charAt(13);
    if(parseInt(val.charAt(16)/2)*2!=val.charAt(16))
    document.all.sex.value='男';
    else
    document.all.sex.value='女';
    //if(val.charAt(17)!=IDCard(val)){
    //document.all.idCard.style.backgroundColor='#ffc8c8';
    //}else{
    ////document.all.idCard.style.backgroundColor='white';
    //}
    document.all.birthday.value=birthdayValue;
    }
    }
    身份证<input type="text" name="idCard1" >
    性别<input type="text" name="sex">
    生日<input type="text" name="birthday" ><input name="" type="button" onClick="showBirthday(document.all.idCard1.value)">