Codeup 기초 100제(1)

만돌이·2022년 2월 28일
0

코딩테스트CodeUp

목록 보기
1/5

1014 : [기초-입출력] 문자 2개 입력받아 순서 바꿔 출력하기

Sanner 클래스 이용

- next : 공백을 기준으로 입력을 받는다.
- nextLine : 줄바꿈(개행문자 \n)을 기준으로 입력을 받는다.
(줄바꿈은 제외한 채로 return | 개행문자가 첫 문자일 경우 개행문자만을 가져옴)
- nexyByte(): byte, 다음 토큰을 byte 타입으로 return
- nextShort(): short, 다음 토큰을 short 타입으로 return
- nextInt(): int, 다음 토큰을 int 타입으로 return / 파라미터로 n진수로 받을 수 o
- nextLong(): long, 다음 토큰을 float 타입으로 return
- nextDouble(): double, 다음 토큰을 double 타입으로 return
- close(): void, Scanner의 사용 종료
- hasNext: 현재 입력된 토큰이 있는경우 return true, 아닐 경우 return false
  • ※주의점 : next, nextInt,nextDouble etc 등을 먼저 작성 후 nextLine을 사용할 시 개행 문자만을 받는 오류 체크

1015 : [기초-입출력] 실수 입력받아 둘째 자리까지 출력하기

String format

//%d (= Integer Formatting) 
int i = 23;

System.out.println(String.format("%d_", i)); //정수 출력
System.out.println(String.format("%5d_", i));
System.out.println(String.format("%-5d_", i));
System.out.println(String.format("%05d_", i));
//숫자 : 글자 길이 설정(해당 길이보다 짧을 경우 공백으로 자리 표현)
//기본 출력은 오른쪽 정렬 출력 / '-'사용의 경우 왼쪽 정렬
//0은 표현할 숫자가 설정한 글자 길이보다 짧을 경우 0으로 채움
//%s (= String Formatting)
int i = 23;

String str = "tete";

System.out.println(String.format("%s_", str));
System.out.println(String.format("%12s_", str));
System.out.println(String.format("%-12s_", str));
System.out.println(String.format("%.2s_", str));
System.out.println(String.format("%-12.2s_", str));
System.out.println(String.format("%12.2s_", str));
%f (= Floating point Formatting)

int i = 23;

double n = 123.45678;

System.out.println(3.4);
System.out.println(n);
System.out.println();

System.out.println(String.format("%f_", 3.4));
System.out.println(String.format("%f_", n));
System.out.println(String.format("%.6f_", n));
System.out.println(String.format("%15f_", n));
System.out.println(String.format("%-15f_", n));
System.out.println(String.format("%.3f_", n));
System.out.println(String.format("%.2f_", n));
System.out.println(String.format("%15.2f_", n));
System.out.println(String.format("%-15.2f_", n));
System.out.println(String.format("%015f_", n));
System.out.println(String.format("%015.2f_", n));

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

'.'(dot) split 할 경우 오류

split 함수의 경우 파라미터를 정규식으로 받는다.
따라서 정규식 표현식인 해당 dot은 그냥 입력 시 다른 뜻으로 전달 가능 (어떤문자도 일치)

=> 따라서 앞에 \\ 를 붙여주어야 함
=> 그 외의

<java 정규식 패턴>

Regular ExpressionDescription
.어떤 문자 1개를 의미
^regex^ 다음 regex로 line을 시작하는지
regex$$ 앞의 regex가 line의 마지막으로 끝나는지
[abc]a, b, c 중의 문자 1개
[abc][vz]a, b, c 중에 문자 1개와 v, z 중에 문자 1개의 조합
[^abc]a, b, c를 제외한 문자 1개
[a-d1-7]ad, 17 사이의 문자 1개
XZ
\d0~9 사이의 숫자, [0-9]와 동일
\D숫자가 아닌 어떤 문자, [^0-9]와 동일
\swhitespace 1개, [\t\n\x0b\r\f]와 동일
\Swhitespace를 제외한 문자
\wAlphanumeric(alphabet, 숫자) 문자, [a-zA-Z_0-9]와 동일
\WAlphanumeric을 제외한 문자(whitespace 등)
\S+whitespace를 제외한 여러 문자
\b단어의 경계(공백)를 찾습니다

0개의 댓글