3 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
public static boolean isInteger(String s) { return isInteger(s,10);}public static boolean isInteger(String s, int radix) { if(s.isEmpty()) return false; for(int i = 0; i < s.length(); i++) { if(i == 0 && s.charAt(i) == '-') { if(s.length() == 1) return false; else continue; } if(Character.digit(s.charAt(i),radix) < 0) return false; } return true;}
public static boolean isInteger(String s, int radix) { Scanner sc = new Scanner(s.trim()); if(!sc.hasNextInt(radix)) return false; // we know it starts with a valid int, now make sure // there's nothing left! sc.nextInt(radix); return !sc.hasNext();}
public static boolean isInteger(String s) { try { Integer.parseInt(s); } catch(NumberFormatException e) { return false; } catch(NullPointerException e) { return false; } // only got here if we didn't return false return true;}

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
添加回答
舉報(bào)