String str=System.in.readline();
try{
  int a=Integer.parseInt(str);
  //继续}catch(NumberFormatException e){
  System.out.println("不是整数!");
}

解决方案 »

  1.   

    for(int i=0;i<str.length;i++){
      char c=str.charAt(i);
      if(c>'9'||c<'0')
        System.out.println("不是整数!");
    }
    //继续
      

  2.   

    厉害,但是,用在我程序中会出错,怎么办?String length = JOptionPane.showInputDialog("请输入毛坯长度(1~500毫米):");//将对话框的值交给length,作者暂时不懂得怎么做有带缺省值的对话框
        for(int i=0;i<str.length;i++){
          char c=str.charAt(i);
          if(c>'9'||c<'0')
            System.out.println("不是整数!");
        }
      

  3.   

    NumberFormatException
    public NumberFormatException()
    Constructs a NumberFormatException with no detail message. 
      

  4.   

    如果需要强制就是不用NumberFormatException,可以使用Character
    public static boolean isDigits(String str) {
            if ((str == null) || (str.length() == 0)) {
                return false;
            }
            for (int i = 0; i < str.length(); i++) {
                if (!Character.isDigit(str.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    上面代码引自jakarta-commons-lang-1.0.2: org.apache.commons.lang.NumberUtils