JAVA
isNumber - 문자열이 숫자로만 이루어졌는지 확인하는 메서드
매운우동
2021. 7. 23. 00:49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class isNumberTest {
static boolean isNumber(String str) {
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch > '9' || ch < '0') {
return false;
}
}
return true;
}
public static void main(String[] args) {
String t = "1034d3";
if (isNumber(t) == false) {
System.out.println("숫자로만 이루어지지 않았습니다.");
} else {
System.out.println("숫자로만 이루어져 있습니다.");
}
}
}
|
cs |
피곤에 쩔어버린 머리로 이리쓰고 저리쓰고 하다보니 작성된 메서드..
사실 완벽하게 이해된 코드가 아니다..
if (ch > '9' || ch < '0') 에서
문자를 어떻게 크기 비교했는지 그게 이해가 잘 안되는 중..
문자랑 문자랑 비교?
아스키코드 값이랑 비교해서 이루어지는건가???
일단 졸린 눈 좀 붙이고 다시 생각해봐야겠다..