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