1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package exception;
 
public class exception_Num {
    public static void main(String[] args) {
        String[] strNum = {"23""12""3.14""998"};
        
        int i = 0;
        try {
            for(i = 0; i < strNum.length; i++) {
                int j = Integer.parseInt(strNum[i]);
                System.out.println(j);
            }
        }
        catch(NumberFormatException e) {
            System.out.println(strNum[i] + "는 정수로 변환할 수 없습니다");
        }
    }
}
 
cs

NumberFormatException 예외처리를 하는 코드이다. 

Integer.parseInt()라는 함수도 볼 수 있다. String 을 int로 변환하는 코드이다.

 

'Java' 카테고리의 다른 글

Java, String(2)  (0) 2019.10.04
Java, String(1)  (0) 2019.10.04
Java, Array  (0) 2019.09.25
Java, for-while, continue, Scanner  (0) 2019.09.25
Java, switch문  (0) 2019.09.25

+ Recent posts