基本上都是在網上找來的, CheckNumber 為已測試的
再加上一個網上的類來
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import java.util.regex.Pattern;
class CheckNumber { public static void main(String[] args) { if (args.length < 1) { System.out.println("Usage: java CheckNumber [number]"); System.exit(1); }
System.out.println(isNumeric_Loop(args[0])); System.out.println(isNumeric_Pattern(args[0])); System.out.println(isNumeric_ASCII(args[0])); System.out.println(isNumeric_TryCatch(args[0])); }
public static boolean isNumeric_Loop(String str) { for (int i = str.length();--i>=0;){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; }
public static boolean isNumeric_Pattern(String str) { Pattern pattern = Pattern.compile("[0-9]+"); return pattern.matcher(str).matches(); }
public static boolean isNumeric_ASCII(String str){ for(int i=str.length();--i>=0;){ int chr=str.charAt(i); if(chr<48 || chr>57) return false; } return true; }
public static boolean isNumeric_TryCatch(String number) { try { Integer.parseInt(number); return true; }catch (NumberFormatException sqo) { return false; } }
}
|
isNumeric(String) 是判斷數字的,包括小數
支持格式:
“33” “+33” “033.30” “-.33” “.33” “ 33.” “ 000.000 “
isInteger(String) 僅是用來判斷整數的
支持格式:
“33” “003300” “+33” “ -0000 “
上面兩函數分別各有兩種方法可以實現
一、一個個字符判斷下去(效率高些)
下面的 iisNumeric(String)、isInteger(String)
二、利用異常:用Integer.parseInt(str),Double.parseDouble(str)解析字符串,若非數字則拋出異常
下面的 isNumericEx(String)、isIntegerEx(String)--其中isIntegerEx(String)最多支持到十位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| package hartech;
public class JMath {
public static boolean isNumeric(String str) { int begin = 0; boolean once = true; if (str == null || str.trim().equals("")) { return false; } str = str.trim(); if (str.startsWith("+") || str.startsWith("-")) { if (str.length() == 1) { return false; } begin = 1; } for (int i = begin; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { if (str.charAt(i) == '.' && once) { once = false; } else { return false; } } } if (str.length() == (begin + 1) && !once) { return false; } return true; }
public static boolean isInteger(String str) { int begin = 0; if (str == null || str.trim().equals("")) { return false; } str = str.trim(); if (str.startsWith("+") || str.startsWith("-")) { if (str.length() == 1) { return false; } begin = 1; } for (int i = begin; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; }
public static boolean isNumericEx(String str) { try { Double.parseDouble(str); return true; } catch (NumberFormatException ex) { return false; } }
public static boolean isIntegerEx(String str) { str = str.trim(); try { Integer.parseInt(str); return true; } catch (NumberFormatException ex) { if (str.startsWith("+")) { return isIntegerEx(str.substring(1)); } return false; } } }
|