알고리즘 공부/CodeUp기초100제

1019 : [기초-입출력] 연월일 입력받아 그대로 출력하기

콩이아부지이 2021. 7. 31. 22:58
728x90

년, 월, 일을 입력받아 지정된 형식으로 출력하는 연습을 해보자.

입력

연, 월, 일이 ".(닷)"으로 구분되어 입력된다.


출력

입력받은 연, 월, 일을 yyyy.mm.dd 형식으로 출력한다.
(%02d를 사용하면 2칸을 사용해 출력하는데, 한 자리 수인 경우 앞에 0을 붙여 출력한다.)


입력 예시   예시 복사

2013.8.5

출력 예시

2013.08.05

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
        String x;
        String[] a; // String[] class 로 선언
        Scanner sc = new Scanner(System.in);
        
        x=sc.nextLine(); // 입력 예  : 2013.8.5
        a=x.split("\\."); // String.split(); 메소드로 인자 만나는 부분까지 잘라줌 인자가 없으면 나머지 출력
        //return값은 String[] type
        int a1 = Integer.parseInt(a[0]); // Integer.parse(); 메소드로 String type int type으로 변환
        int a2 = Integer.parseInt(a[1]);
        int a3 = Integer.parseInt(a[2]);
        
        System.out.printf("%04d.%02d.%02d", a1,a2,a3);
        
	}
}
728x90