본문 바로가기
JAVA

8일차//[java] 문자열에서 모두 숫자로만 이루어져 있는지 파악 (method사용)

by aesup 2021. 1. 21.
728x90
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
public class report4 {
    // 주어진 문자열이 숫자로 이뤄져있는지 숫자로 이뤄지지않았는지
    static boolean isNumber(String str) {
        boolean a = true;
        int str_1;
        for (int i = 0; i < str.length(); i++) {
            str_1 = (int) str.charAt(i);
            if (str_1 < 48 || str_1 > 57) {
 
                a = false;
            } else {
 
                a = true;
            }
        }
 
        if (a == true) {
            System.out.println("숫자로만 이뤄져있습니다.");
        } else {
            System.out.println("문자열도 포함되었습니다.");
 
        }
 
        return a;
 
    }
 
    public static void main(String[] args) {
 
        String str = "123";
        System.out.println(str + "는 숫자입니까? " + isNumber(str));
        str = "1234o";
        System.out.println(str + "는 숫자입니까? " + isNumber(str));
 
    }
}
 
cs

 

728x90