입출력 - Scanner와 개행 문자

	public static void main(String[] args) {
		String a = "23\nasd\nbc\n";
		Scanner sc = new Scanner(a);
		System.out.print(sc.nextInt());
		String b = sc.nextLine();
		System.out.print(b);
		System.out.print(sc.nextLine());
		System.out.print(sc.nextLine());
		
		System.out.println(b.equals(""));
		sc.close();
	}
}

next(), nextInt(), etc

문자열에서 \n 직전까지만 읽는다. 만약 문자열 가장 앞이 \n일 경우 읽을 값이 없어 예외가 발생한다.

nextLine()

문자열에서 \n 까지 읽지만 \n은 버린다.

좋은 웹페이지 즐겨찾기