๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/๋ฐฑ์ค€

์ž๋ฐ” ์ž…๋ ฅ์— ๊ด€ํ•œ ๊ณ ์ฐฐ (Scanner, BufferedReader)

by ๋ฝ€์งœ๊ผฌ 2025. 1. 14.
728x90
๋ฐ˜์‘ํ˜•

 

์Šคํ„ฐ๋””์—์„œ ์ˆ™์ œ๋ฅผ ํ•˜๋‚˜ ๋ฐ›์•˜๋‹ค.

โ€ป ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ๊ณผ์ • Java์—์„œ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ์— ์žˆ์–ด์„œ,
  1. ๊ฐ„๋‹จํ•œ ์ž…๋ ฅ + ๋‹ค์–‘ํ•œ ์ถœ๋ ฅ ํ˜•ํƒœ
  2. ๋ณต์žกํ•œ ์ž…๋ ฅ + ์ถœ๋ ฅ ํ˜•ํƒœ
  3. ๊ฐ„๋‹จํ•œ ์ž…๋ ฅ + ๊ฐ„๋‹จํ•œ ์ถœ๋ ฅ
์„ธ๊ฐ€์ง€ ์ผ ๋•Œ memory ์‚ฌ์šฉ๋Ÿ‰๊ณผ ์ฒ˜๋ฆฌ ์†๋„์— ๋”ฐ๋ผ์„œ ์–ด๋–ค๊ฑธ ์‚ฌ์šฉํ•ด์•ผ ํšจ์œจ์ ์ธ์ง€ ๊ณ ๋ฏผํ•ด๋ณด๋Š” ์ˆ™์ œ์ž…๋‹ˆ๋‹ค.
๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ ์ž๋ฐ”์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์šฐ๋Š” ๊ฒƒ์€
Scanner ์ผํ…๋ฐ, BufferedReader ์™€ StringBuilder+ @ ๊นŒ์ง€ ๋ฏธ๋ฆฌ ๊ณต๋ถ€ํ•ด๋ณด์‹œ๊ณ ,
์–ด๋–ป๊ฒŒ ํš๊ธฐ์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๊ณผ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ์ค„์˜€๋Š”์ง€์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•ด๋ณด๋Š” ์ˆ™์ œ์ž…๋‹ˆ๋‹ค.

 

๋ฐฑ์ค€ 1000๋ฒˆ ๋ฌธ์ œ์™€ ํ•จ๊ป˜ ๊ณ ์ฐฐ์„ ํ•ด๋ณด์•˜๋‹ค.

 


์ผ๋‹จ Scanner์™€ BufferedReader๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

Scanner scan = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

1. Scanner

nextLine() ์€ ๋ฌธ์ž์—ด์„, nextInt()๋Š” ์ •์ˆ˜๋ฅผ, nextDouble() ์€ ๋”๋ธ”ํ˜•์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

 

๋ฐฑ์ค€ 1000๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ฉด์„œ ์•Œ์•„๋ณด์ž.

import java.util.Scanner;

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

        int A = scanner.nextInt();
        int B = scanner.nextInt();
        System.out.println(A+B);
    }
}

 

Scanner๋Š” ๊ณต๋ฐฑ(์ŠคํŽ˜์ด์Šค, ํƒญ, ์ค„๋ฐ”๊ฟˆ)์„ ๊ธฐ์ค€์œผ๋กœ ์ž…๋ ฅ๊ฐ’์„ ๊ตฌ๋ถ„

→ ๋”ฐ๋กœ splitํ•˜๊ฑฐ๋‚˜ ํ† ํฐํ™” ํ•  ํ•„์š” ์—†๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ฉ”๋ชจ๋ฆฌ์™€ ์‹œ๊ฐ„์ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์†Œ์š”๊ฐ€ ๋˜์—ˆ๋‹ค.

 

2. BufferedReader

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        String[] tlist = input.split(" ");

        // ์ŠคํŠธ๋ง์œผ๋กœ ๋ฆฌํ„ด๊ฐ’ ๊ณ ์ •๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ํ˜•๋ณ€ํ™˜ ํ•„์š”
        int A = Integer.parseInt(tlist[0]);
        int B = Integer.parseInt(tlist[1]);
        System.out.println(A+B);
    }
}

 

๋ฌธ์ œ์—์„œ A์™€ B๋Š” ๊ฐ™์€์ค„์—์„œ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

๋‹ค๋งŒ Scanner์™€ ๋‹ค๋ฅธ์ ์€

์ž๋™์œผ๋กœ ํ† ํฐํ™”๊ฐ€ ์•ˆ๋˜๊ณ  ํ•œ์ค„๋กœ ์ฝ์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์—, ์ˆ˜๋™ ํ† ํฐํ™”๋ฅผ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

 

๋ฉ”๋ชจ๋ฆฌ์™€ ์‹œ๊ฐ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์†Œ์š”๋˜์—ˆ๋‹ค.

 

๊ฒฐ๋ก  : BufferedReader๊ฐ€ ๋” ๋นจ๋ž๋‹ค. ์ด์œ ๋Š”?

  1. Scanner : ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์œผ๋ฉฐ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด ํŒŒ์‹ฑํ•œ๋‹ค.
    1. nextInt()๊ฐ€ ์ž…๋ ฅ๊ฐ’์ด ์ •์ˆ˜์ธ์ง€ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์„ ํฌํ•จํ•œ๋‹ค.
  2. BufferedReader :
    • just ๋ฌธ์ž์—ด ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ๋ฐ˜ํ™˜ํ•ด ์ถ”๊ฐ€์ ์ธ ๋ฐ์ดํ„ฐ ๋ถ„์„์ด๋‚˜ ๋ณ€ํ™˜์ž‘์—… ์—†์Œ
    • ๋ฐ์ดํ„ฐ๋ฅผ ๋ฒ„ํผ์— ์ €์žฅํ•œ ํ›„ ์ฒ˜๋ฆฌ, ํ•œ ๋ฒˆ์— ํฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€์„œ ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ๋น ๋ฆ„.

๋‚˜์˜ ๊ฒฐ๋ก 

1. ๊ฐ„๋‹จํ•œ ์ž…๋ ฅ + ๋‹ค์–‘ํ•œ ์ถœ๋ ฅ ํ˜•ํƒœ
      → ์—๋Š” Scanner

  • ์ถœ๋ ฅ ์ž‘์—…์ด ๋งŽ๋‹ค๋ฉด StringBuilder

StringBuilder

String ๊ฐ์ฒด๋ผ๋ฆฌ ๋”ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น๊ณผ ํ•ด์ œ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š”๋ฐ, ๋ง์…ˆ ์—ฐ์‚ฐ์ด ๋งŽ์•„์ง„๋‹ค๋ฉด ์„ฑ๋Šฅ์ ์œผ๋กœ ์ข‹์ง€ ์•Š๋‹ค.
๊ทธ๋ƒฅ ์•ฝ๊ฐ„ ์ถœ๋ ฅ๊ฐ’ ์กฐ์ž‘ ๋Š๋‚Œ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค ์ƒ๊ฐํ•˜๋ฉด ๋ ๋“ฏ.
๋ฌธ์ž์—ด์„ ํšจ์œจ์ ์œผ๋กœ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค์ด๋‹ค

 

2. ๋ณต์žกํ•œ ์ž…๋ ฅ + ์ถœ๋ ฅ

   → ์—๋Š” BufferedReader

 

3. ๊ฐ„๋‹จํ•œ ์ž…๋ ฅ + ๊ฐ„๋‹จํ•œ ์ถœ๋ ฅ

    → ์—๋Š” ์†๋„๊ฐ€ ์ค‘์š”ํ•˜๋‹ค๋ฉด BufferedReader ๊ฐ€ ํšจ์œจ์ ์ด์ง€ ์•Š์„๊นŒ.

    ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ๊ฐ„ํŽธํ•˜๊ฒŒ Scanner๊ฐ€ ์ข‹์ง€ ์•Š์„๊นŒ ํ•˜๊ณ  ์ƒ๊ฐํ•ด๋ณธ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•