-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIsNumeric.java
More file actions
66 lines (59 loc) · 1.94 KB
/
IsNumeric.java
File metadata and controls
66 lines (59 loc) · 1.94 KB
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
package com.company;
import org.junit.Test;
public class IsNumeric {
public boolean isNumeric(char[] data) {
if (data == null || data.length == 1) return false;
int index = 0;
//是否有符号位存在,存在则跳过
if (data[0] == '+' || data[0] == '-') {
index++;
}
//解析A部分是否正确
while (index < data.length) {
//查看是否“.”,如果是直接跳出。
if (data[index] == '.') {
index++;
break;
}
//查看是否“e|E”,如果是直接跳出,不移动下标在B部分进行处理。
if (data[index] == 'e' || data[index] == 'E') break;
if (data[index] >= '0' && data[index] <= '9') {
index++;
continue;
}
return false;
}
//解析B部分是否正确
while (index < data.length) {
//查看是否“e|E”,如果是直接跳出。
if (data[index] == 'e' || data[index] == 'E') {
index++;
if (index >= data.length) return false;
break;
}
if (data[index] >= '0' && data[index] <= '9') {
index++;
continue;
}
return false;
}
//查看c部分有没有正负号,如果有跳过
if (index < data.length &&
(data[index] == '+' || data[index] == '-')) {
index++;
}
//解析C部分是否正确
while (index < data.length) {
if (data[index] >= '0' && data[index] <= '9') {
index++;
continue;
}
return false;
}
return true;
}
@Test
public void test() {
System.out.println(isNumeric("2e".toCharArray()));
}
}