TIL 2022-01-01

그린·2022년 2월 2일
0

TIL

목록 보기
1/47

1. 오늘 학습한 내용

코드업 기초 100제 1001 ~ 1020번 풀기

사용 언어 : 자바

2. 오늘 알게 된 내용

  • float형 소수점 이하 자리 0으로 채우기

    (1012번 문제 관련)

    1.54로 입력되었을 때 1.540000으로 출력되어야 정답으로 인정되었지만 처음에 float형을 그대로 출력하였기 때문에 1.54로 출력되었다. 검색해본 결과 다음과 같이 2가지 방법을 알게 되었다.

    1. DecimalFormat을 활용

      import java.text.DecimalFormat;
      import java.util.Scanner;
      
      public class Main {
          public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
              float f = scanner.nextFloat();
              DecimalFormat form = new DecimalFormat("#.000000");
              System.out.println(form.format(f));
          }
      }

      위와 같이 DecimalFormat을 #.000000 으로 포맷을 설정해서 소수점 이하에서는 값이 없으면 0으로 출력하게 한 후, format(실수) 메서드를 이용해 원하는 방식으로 6자리까지 나오게 출력할 수 있다.

      출처 : https://javafactory.tistory.com/899

    2. String.format 이용

      import java.util.Scanner;
      
      public class Main {
      public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          float f = scanner.nextFloat();
          System.out.println(String.format("%.6f", f));
          }
      }

      위와 같이 String.format() 메서드를 이용하고 소수점 6자리까지 나타내는 방식을 이용할 수 있다.

      출처 : https://settembre.tistory.com/160

      +) 추가

      000907 이런 식으로 나타내고 싶으면 %06f 이렇게 나타내면 된다.

  • ":" 을 기준으로 나눠서 입력받기
    (1018번 문제)

    1. BufferedReader를 활용해 나누기

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      
      public class Main {
          public static void main(String[] args) {
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
              try {
                  String[] s = br.readLine().split(":");
                  int hour = Integer.parseInt(s[0]);
                  int min = Integer.parseInt(s[1]);
                  System.out.println(hour+":"+min);
              } catch (IOException e) {
              }
          }
      }

      다른 분들의 코드를 보고 알게 된 것인데 꼭 int형으로 받지 않아도 되는 것 같다.

      출처 : https://mygumi.tistory.com/78
      (다양한 입력 방식 설명)

    2. Scanner를 활용해 나누기

      import java.util.Scanner;
      
      public class Main {
          public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
              String time[] = scanner.next().split(":");
              System.out.println(time[0] + ":" + time[1]);
          }
      }

      잘 몰랐었는데 Scanner에도 split 기능이 있다는 것을 알게 되었다.

      출처 : https://settembre.tistory.com/166

  • .으로 split 하는 방법

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Main {
        public static void main(String[] args) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try {
                String[] s = br.readLine().split("\\.");
                int year = Integer.parseInt(s[0]);
                int month = Integer.parseInt(s[1]);
                int day = Integer.parseInt(s[2]);
                System.out.printf("%04d.%02d.%02d", year, month, day);
            } catch (IOException e) {
            }
        }
    }

    .으로 split하려면 "\\."으로 해야 한다.

    출처 : https://stackoverflow.com/questions/14833008/java-string-split-with-dot

3. 느낀 점

기초 부분을 풀었지만 은근 모르는 게 많았다. 그래도 목표한 양은 다 풀었고 새로 알아가는 부분들도 있어서 뿌듯하다. 모르는 걸 바로 받아들이고 바로 찾아보고 내 것으로 만들기 위해 앞으로 더 꾸준히 문제를 풀고 해결하기 위해 노력해야겠다.

profile
기록하자

0개의 댓글

Powered by GraphCDN, the GraphQL CDN