반응형
🖥 나의 풀이
class Solution {
public boolean solution(String s) {
if ((s.length() == 4 || s.length() == 6) && s.matches("[0-9]+")) {
return true;
}
else {
return false;
}
}
}
🖥 다른 풀이
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;
}
}
📚 참고 자료
https://alisyabob.tistory.com/297
[JAVA] 문자열 찾기 (Contains, IndexOf, Matches)
[JAVA] 문자열 찾기 3가지 방법 Contains, IndexOf, Matches 설명 Contains : 문자열에 검색하고자 하는 문자가 있는지 확인 - 결과 : true / false로 리턴 예시) public class Contains { public static v..
alisyabob.tistory.com
https://myhappyman.tistory.com/183
JAVA - 문자열에 숫자만 존재하는지 확인하기
데이터를 검증하는 과정에서 문자열에 존재하는 데이터가 숫자만 있어야 하는 경우가 필요했는데, 이후 substring등으로 특정 형태로 변경해야해서 특히 검증을 꼼꼼하게 진행했습니다. 아래와
myhappyman.tistory.com
반응형
'코딩테스트 연습 > JAVA' 카테고리의 다른 글
[JAVA/알고리즘] 나머지가 1이 되는 수 찾기 (0) | 2022.03.10 |
---|---|
[JAVA/알고리즘] 정수 제곱근 판별 (0) | 2021.07.23 |
[JAVA/알고리즘] 자릿수 더하기 (0) | 2021.07.21 |
[JAVA/알고리즘] 콜라츠 추측 (0) | 2021.07.19 |
[JAVA/알고리즘] 폰켓몬 (0) | 2021.07.18 |