exception=java.lang.NumberFormatException: For input string: "숫자"
원인
1. 정수아닌 문자 포함 되었을 때
String str = "123o";
System.out.println(Integer.parseInt(str));
2. int 범위보다 큰 정수일 때
String str = "123123123123123123123123";
System.out.println(Integer.parseInt(str));
3. null 입력 시
String str= null;
System.out.println(Integer.parseInt(str));
4. 문자열에 공백이 있을 경우
String str = " 123 ";
System.out.println(Integer.parseInt(str));
- int를 string으로 변경 시에도 동일.
> try-catch 사용하여 Exception 처리
public class sample {
public static void main(String[] args) throws Exception{
String str = "";
try{
str = "123o";
System.out.println(Integer.parseInt(str));
}catch(NumberFormatException e){
str = "1230"; // 디폴트 값이 있으면 설정
System.out.println(Integer.parseInt(str));
}catch(Exception e){
e.printStackTrace();
}
}
}
내 경우 공백 때문에 발생한 에러임.
공백 replaceAll 해줌
String newDate = words[5].replaceAll("\\s", "");