[Java] ๐Ÿ’พ EOF (End Of File)

shrยท2022๋…„ 6์›” 3์ผ
0

Concept

๋ชฉ๋ก ๋ณด๊ธฐ
11/13
post-thumbnail

EOF


ํŒŒ์ผ์˜ ๋ (End Of File), ์ฆ‰ ๋” ์ด์ƒ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. IDE๋‚˜ ํ„ฐ๋ฏธ๋„์—์„œ EOF๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ ค๋ฉด ์œˆ๋„์šฐ ๊ธฐ์ค€ ctrl+z๋กœ ์ž…๋ ฅ์„ ์ฃผ์–ด์•ผ ํ•œ๋‹ค.


์‚ฌ์šฉ๋ฒ•


Scanner

import java.util.Scanner;
 
public class Main {
	public static void main(String args[]){
		Scanner sc=new Scanner(System.in);
		while(sc.hasNextInt()){
			System.out.println(sc.nextLine());
		}	
		sc.close();
	}
}

hasNext()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ž…๋ ฅ๋œ ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (hasNextInt()๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค. ์ •์ˆ˜์ผ ๊ฒฝ์šฐ์—๋Š” true, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.)


BufferedReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main{
	public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {
        	/* ๋กœ์ง ์ „๊ฐœ */
        }
    }
}

br.readLine()์œผ๋กœ ์ž…๋ ฅ ๊ฐ’์„ ๋ฐ›๋‹ค๊ฐ€, ์ž…๋ ฅ ๊ฐ’์ด null์ด ๋˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•œ๋‹ค.


์ฐธ๊ณ  ์ž๋ฃŒ


[๋ฐฑ์ค€] 10951๋ฒˆ : A+B - 4 - JAVA [์ž๋ฐ”]
EOF์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž (JAVA)

profile
๋ชปํ•˜๋‹ค ๋ณด๋ฉด ์ž˜ํ•˜๊ฒŒ ๋˜๋Š” ๊ฑฐ์•ผ ๏ฝฅแด—๏ฝฅฬฅฬฅฬฅ

0๊ฐœ์˜ ๋Œ“๊ธ€