// 判断是否数值的函数
      public static boolean isNumeric(String inputstr)
      {
          boolean rv=true;
          if(inputstr!=null)
          {
              byte[] bytearray=inputstr.getBytes();
              int bytelength=bytearray.length;
              int i=0;
              while(i<bytelength&&rv)
              {
                  if(bytearray[i]<48||bytearray[i]>57)
                  {
                    rv=false;
                  }
                  i=i+1;
              }
          }
          else
          {
              rv=false;
          }
          return rv;
      }

解决方案 »

  1.   

    // 直接取值
    int syear = Integer.parseInt(request.getAttribute("holid_year"));
      

  2.   

    String num = "1234";
    boolean isNum = false;
    try{
      int n = Integer.parseInt(num)
    } catch(Exception e) {
      isNum = true;
    }
    if(isNum) {
      System.out.println("num is not a Number");
    } else {
      System.out.println("num is a Number");
    }
      

  3.   

    同意楼上的方法。楼主写一个函数就好public boolean isNumber(String s){
      try{
         Integer.parseInt(s);
         return true;
      }catch(Exception e){
         return false;
      }
    }如果需要判断这个String是否double, 只要把上面的Integer改成Double就可以。然后在楼主代码??的地方用 isNumber(syear)  代替就可以
      

  4.   

    我认为楼主的代码有问题。
    int year = 2003; 
    String syear = (String)request.getAttribute("holid_year");
    if(syear != null && syear.length() >1 && ??){ 
         year = Integer.parseInt(syear); 
    }
    在你的代码中syear如果是空的话,你的syear.length()就会抛出异常。
     public String intstr(String value)
           {
    String message=new String();
            for(int i=0;i<value.length();i++)
            {
              if((value.charAt(i)>'9')||(value.charAt(i)<'0'))
              {message="不是整形"; break;}
            else
            {message="是整形";}
            }
        return message;
       }
    你可以根据返回的的message判断是不是整形
      

  5.   


    程序应该这样写:
    int year = 2003; 
    String syear = (String)request.getAttribute("holid_year");try{
                if (syear != null && syear.length() > 1) {
                    year = Integer.parseInt(syear);
                }
        }catch(NumberFormatException e){
                .....
        }
      

  6.   

    import java.util.regex.*;public class test {
        public static void main(String[] args) 
        {
            String input = args[0];
            String ps="\\d+" ;
            Pattern p = Pattern.compile(ps);
            Matcher m = p.matcher(input);
            if(m.matches()) {
                System.out.println("ok");
            }     
    else {
                System.out.println("shit");        }
        }}
      

  7.   

    非常感谢各位.我的技术问题得到解决.代码如下:
    String syear = (String)request.getAttribute("holid_year");
    if(syear != null && syear.length()>1&&syear.compareTo("9999")<0&&syear.compareTo("0000")>0){
    year = Integer.parseInt(syear);
    }
    小弟有礼了 fan
      

  8.   

    不是很多书都说不要用Exception来做判断的吗??