HackerRank로 Java 언어를 다시 차근차근 공부하고 있는데, 입출력을 받을 때 특이한 경우를 배우게 되었다.
알고리즘 공부로 Python 쓰면서 이런 경우는 readline을 할 때 '\n'이 같이 읽힌다는 거랑 비슷한 것 같다. C언어도!
Scanner의 nextInt, nextDouble 뒤에 next 혹은 nextline을 사용할 때
엔터 값('\n')을 읽어들이기 때문에 이를 주의해서 사용해야한다.
그래서 nextInt, nextDouble 뒤에 또 nextline을 읽어서 '\n'값을 없애주자!
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
'java' 카테고리의 다른 글
[우테코][IntelliJ] Java와 Gradle 버전 맞추기 (0) | 2022.10.26 |
---|---|
[Intellij] 한글깨짐 (0) | 2022.05.31 |