티스토리 뷰
https://programmers.co.kr/learn/courses/30/lessons/12918
import java.util.*;
class Solution {
public boolean solution(String s) {
boolean answer = true;
if(s.length() == 4 || s.length() == 6) {
for(int i=0; i<s.length(); i++) {
if(!Character.isDigit(s.charAt(i))) {
answer = false;
break;
}
}
} else {
answer = false;
}
return answer;
}
}
try~catch 문 사용
class Solution {
public boolean solution(String s) {
if(s.length() == 4 || s.length() == 6){
try{
int x = Integer.parseInt(s);
return true;
} catch(NumberFormatException e){
return false;
}
} else {
return false;
}
}
}
정규식 사용
class Solution {
public boolean solution(String s) {
return s.matches("^[0-9]{4}|{6}$");
}
}