方法:
/** * check a string whether is numeric or not * @param String str * @return boolean true or false */ public static boolean is_numeric(String str) { try { Double d= Double.valueOf(str); } catch (java.lang.Exception e) { return false; } return true; }
测试代码:
public class Test { /** * test the is_numeric method * @param args */ public static void main(String[] args) { String s = "123.123"; if (is_numeric(s)) { System.out.println(s+" is numeric"); } else { System.out.println(s+" not numeric"); } String a = "123"; if (is_numeric(a)) { System.out.println(a+" is numeric"); } else { System.out.println(a+" not numeric"); } String b = "12ad"; if (is_numeric(b)) { System.out.println(b+" is numeric"); } else { System.out.println(b+" not numeric"); } } /** * check a string whether is numeric or not * @param String str * @return boolean true or false */ public static boolean is_numeric(String str) { try { Double d= Double.valueOf(str); } catch (java.lang.Exception e) { return false; } return true; } }
测试输出:
123.123 is numeric 123 is numeric 12ad not numeric
时间: 2024-09-20 11:50:36