티스토리 뷰

Scanner sc = new Scanner(System.in);

sc.nextInt()는 정수를 바로 받을 수 있는 메소드입니다.

그.러.나

sc.nextInt()를 사용하고 바로 다음 줄에 sc.nextLine()을 사용 시 sc.nextLine();이 실행되지 않는 문제가 있습니다!

이건 sc.nextInt();를 실행 할때 enter키를 누르는데 그 enter키를 sc.nextLine(); 이 한줄로 인식하기 때문입니다~

즉 개행문자 때문이라고 말할 수 있죠~

이 문제를 해결 하기 위해서는 sc.nextLine();를 한번 더 사용하는 방법이 있어요.

1
2
3
4
5
6
7
8
9
10
public class for_02 {
    public static void main(String[] args){
         Scanner sc = new Scanner(System.in);
         int num = sc.nextInt();
         System.out.println(num);
         sc.nextLine(); //엔터(개행문자)를 제거하기 위한 것
         String num2 = sc.nextLine();
         System.out.println(num2);
    }
}
cs

 

댓글