scanner

  • Scanner : ํ‚ค๋ณด๋“œ ์ž…๋ ฅ์ด๋‚˜ ํŒŒ์ผ ๋“ฑ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ ๊ฐ’์„ ๋ฐ›์•„์˜ค๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค

package sec04.chap07;

import java.util.Scanner;

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

        String str1 = sc.next();
        String str2 = sc.next();
        String str3 = sc.nextLine();

        System.out.println("str1: " + str1);
        System.out.println("str2: " + str2);
        System.out.println("str3: " + str3);
    }
}

next : ์ŠคํŽ˜์ด์Šค๋ฅผ ๋น„๋กฏํ•œ ๊ณต๋ฐฑ ๋‹จ์œ„๋กœ ๋Š์–ด์„œ (ํ† ํฐ์œผ๋กœ) ๋ฌธ์ž์—ด์„ ๋ฐ›์Œ

nextLine : ์ค„๋ฐ”๊ฟˆ ๋‹จ์œ„๋กœ ๋Š์–ด์„œ ๋ฌธ์ž์—ด์„ ๋ฐ›์Œ

package sec04.chap07;

import java.util.Scanner;

public class Ex02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");

        while (sc.hasNext()) {
            String nextWord = sc.next();
            if (nextWord.equalsIgnoreCase("quit")) break;
            System.out.println("์ž…๋ ฅ๊ฐ’: " + nextWord);
        }

        //  โญ ์Šค์บ๋„ˆ์˜ ์‚ฌ์šฉ์ด ๋๋‚˜๋ฉด OS์ž์›์„ ๋ฐ˜ํ™˜
        //  ํŒŒ์ผ ๋“ฑ์œผ๋กœ๋ถ€ํ„ฐ ์ฝ์–ด์˜ค๋Š”๋ฐ ์‚ฌ์šฉ์‹œ ํ•„์ˆ˜
        sc.close();

    }
}

Last updated