Java IsNumber 的四個方法


基本上都是在網上找來的, 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;	// 載入正則用的 API

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();
}

// 用迴圈方式,讀出每個輸入文字,並將其轉為 ASCII 碼, 再比對 ASCII 碼數字的範圍
// 此方法和 isNumeric_Loop 相類似
public static boolean isNumeric_ASCII(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr&lt;48 || chr>57) return false;
}
return true;
}

// 利用 Try Catch 的方式,找出整數轉換時的例外事例判別
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;

/**
* <p>Copyright: Copyright (c) 2006 hartech.cn</p>
*
* <p>Website: www.hartech.cn</p>
*
* <p>Page: http://www.hartech.cn/blog/blogview.asp?logID=73 </p>
*
* @author [email protected]
* @version 1.0
*/
public class JMath {


/**
* support Numeric format:<br />
* "33" "+33" "033.30" "-.33" ".33" " 33." " 000.000 "
* @param str String
* @return boolean
*/
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) {
// '.' can only once
once = false;
}
else {
return false;
}
}
}
if (str.length() == (begin + 1) && !once) {
// "." "+." "-."
return false;
}
return true;
}

/**
* support Integer format:<br>
* "33" "003300" "+33" " -0000 "
* @param str String
* @return boolean
*/
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;
}


/**
* use Exception
* support Numeric format:<br>
* "33" "+33" "033.30" "-.33" ".33" " 33." " 000.000 "
* @param str String
* @return boolean
*/
public static boolean isNumericEx(String str) {
try {
Double.parseDouble(str);
return true;
}
catch (NumberFormatException ex) {
return false;
}
}

/**
* use Exception
* support less than 11 digits(&lt;11)<br />
* support Integer format:<br />
* "33" "003300" "+33" " -0000 " "+ 000"
* @param str String
* @return boolean
*/
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;
}
}
}