본문 바로가기
프로그래머스

프로그래머스 - 문자열 다루기 기본 JAVA

by minsol Kim 2022. 2. 26.
  • 문자열 다루기 기본
문제 설명

문자열 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하세요. 예를 들어 s가 "a234"이면 False를 리턴하고 "1234"라면 True를 리턴하면 됩니다.

제한 사항
  • s는 길이 1 이상, 길이 8 이하인 문자열입니다.
입출력 예
s                                                                                       return
"a234" false
"1234" true
public class StringSolution {
	public boolean Solution(String s) {
		boolean answer = true;
		try {
			Integer.parseInt(s);
			if(s.length() !=4 && s.length() !=6) {
				answer = false;
			}
		}catch(Exception e){
			answer=false;
		}
		return answer;
	}

	public static void main(String[] args) {
		StringSolution s = new StringSolution();
		String a = "1234";
		System.out.println(s.Solution(a));
	}

}

프로그래머스 코드

class Solution {
    public boolean solution(String s) {
        boolean answer = true;
        try{
            Integer.parseInt(s);
            if(s.length() !=4 && s.length() !=6){
                answer = false;
            }
        }catch(Exception e){
            answer = false;
        }
        return answer;
    }
}

 

댓글