김찬진의 개발 블로그

23/02/02 [next(), nextLine()?] 본문

1일1배움/Java

23/02/02 [next(), nextLine()?]

kim chan jin 2023. 2. 2. 03:55

next()

공백문자 또는 개행문자 이전까지의 문자 또는 문자열을 반환

반환할 때 공백문자나 개행문자는 버퍼에서 제거

 

nextLine()

개행문자 이전까지의 문자 또는 문자열을 개행문자를 포함하여 반환

버퍼에 남아있는 문자가 없으므로 다음 입력 때 걱정할 필요없음

 

import java.util.Scanner;

public class Test{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

//        // Case1
//        // a b
//        // 1
//        // = a\sb\n1
//        String s1 = sc.next(); // a
//        String s2 = sc.nextLine(); // \sb
//        int i1 = sc.nextInt(); // 1
//
//        System.out.println(s1);
//        System.out.println(s2);
//        System.out.println(i1);

//        // Case2
//        // a 1 b
//        // = a\s1\sb\n
//        String s1 = sc.next(); // a
//        int i1 = sc.nextInt(); // 1 (int이니깐 \s 무시)
//        String s2 = sc.nextLine(); // \sb
//
//        System.out.println(s1);
//        System.out.println(i1);
//        System.out.println(s2);

//        // Case3
//        // a 1
//        // = a\s1
//        String s1 = sc.next(); // a
//        int i2 = sc.nextInt(); // 1 (int이니깐 \s 무시)
//        System.out.println(s1);
//        System.out.println(i2);

//        // CASE4
//        // a b
//        // 1
//        // = a\sb\n1
//        String s1 = sc.next(); // a
//        String s2 = sc.nextLine();// \sb
//        int i1 = sc.nextInt(); // 1 (int이니깐 \n 무시)
//        System.out.println(s1);
//        System.out.println(s2);
//        System.out.println(i1);

        // CASE4 SOLUTION
        // a b 1
        // = a\sb\s1
        String s1 = sc.next(); // a
        String s2 = sc.next();// b
        int i1 = sc.nextInt(); // 1 (int이니깐 \n 무시)
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(i1);
    }
}

 

 

 

Comments