# [Java] PS์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ž…๋ ฅ ํด๋ž˜์Šค์— ๋Œ€ํ•ด
Study Repository

[Java] PS์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ž…๋ ฅ ํด๋ž˜์Šค์— ๋Œ€ํ•ด

by rlaehddnd0422

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด์—์„œ ํ‘œ์ค€ ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์ข‹์€ ์ „๋žต์€ ์•„๋‹™๋‹ˆ๋‹ค.

PS์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฐ” ์ž…๋ ฅ ํด๋ž˜์Šค ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์•Œ์•„๋ด…์‹œ๋‹ค.

 

๋Œ€ํ‘œ์ ์œผ๋กœ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ž…๋ ฅ ํด๋ž˜์Šค์—๋Š” Scanner, BufferedReader๊ฐ€ ์žˆ๊ณ , ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ํ† ํฌ๋‚˜์ด์ง•ํ•˜์—ฌ ์Šฌ๋ผ์ด์Šคํ•  ์ˆ˜ ์žˆ๋Š” StringTokenizer๊ฐ€, ์—ฌ๋Ÿฌ ๋ฌธ์ž์—ด๋“ค์„ ๋ชจ์•„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ํ•œ ๊ณณ์— ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ๋Š” StringBuilder๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

Scanner

next + ์ž๋ฃŒํ˜•์„ ์ด์šฉํ•ด ์ž…๋ ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ , hasNext + ์ž๋ฃŒํ˜•์„ ใ…์ด์šฉํ•ด ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•์ด ์žˆ๋Š”์ง€ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ธฐ๋ณธ์ ์ธ ์„ ์–ธ ๋ฐ ์‚ฌ์šฉ

public class ScannerTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        double d = sc.nextDouble();
        String str = sc.next();

        System.out.println("a = " + a);
        System.out.println("d = " + d);
        System.out.println("str = " + str);
    }
}

hasNextXXX์„ ์ด์šฉํ•ด ์ž…๋ ฅ์—์„œ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋Š” ๋™์•ˆ ๊ณ„์† ์ž…๋ ฅ ๋ฐ›๊ธฐ 

public class ScannerTest1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int k = sc.nextInt();
            System.out.println("k = " + k);
        }
    }
}

์ •์ˆ˜์™€ ๋ฌธ์ž์—ด ๋™์‹œ ์ฒ˜๋ฆฌ

public class ScannerTest2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt(); // ์ •์ˆ˜ + '\n'
        // sc.nextLine();
        String s = sc.nextLine();  // s์— ์ค„๋ฐ”๊ฟˆ๋ฌธ์ž๊ฐ€ ์ž๋™์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
        System.out.println("i = " + i);
        System.out.println("s = " + s);
    }
}

์ด ์ฝ”๋“œ์—์„œ 1์„ ์ž…๋ ฅํ•˜๊ณ  ์ž…๋ ฅ์„ ๋งˆ์น˜๊ธฐ ์œ„ํ•ด ์—”ํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฒ„ํผ์— '1\n'์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด ๋•Œ String s์— ๋ฒ„ํผ์˜ ์ค„๋ฐ”๊ฟˆ๋ฌธ์ž๊ฐ€ ์‚ฝ์ž…๋˜๊ณ  ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ค„๋ฐ”๊ฟˆ์„ ์ž…๋ ฅ๋ฐ›๋Š” ์ฝ”๋“œ์ธ scanner.nextLine(); ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์–ด์•ผ ์˜ฌ๋ฐ”๋ฅธ ์˜๋„๋Œ€๋กœ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Scanner์˜ ๋‹จ์  : ์†๋„๊ฐ€ ๋งค์šฐ ๋Š๋ฆฌ๊ณ  ์ž…๋ ฅ์„ ๋งŽ์ด ๋ฐ›์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๋ณ„๋„์˜ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

BufferedReader

  • Scanner์˜ ๋Š๋ฆฐ ์†๋„ ๊ฐœ์„ ๋œ ์ž…๋ ฅ ํด๋ž˜์Šค
  • read(), readLine()๋งŒ ์žˆ๊ธฐ ๋–„๋ฌธ์— ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์ •์ˆ˜ํ˜•์€ ํŒŒ์‹ฑ์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
public class BufferedReaderTest0 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");

        int result = Integer.valueOf(line[0]) + Integer.valueOf(line[1]);

        System.out.println("result = " + result);
    }
}

 

StringTokenizer

  • ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ํ† ํฐ์œผ๋กœ ์ž˜๋ผ์•ผ ํ•  ๋•Œ ์œ ์šฉํ•œ ํด๋ž˜์Šค
  • BufferedReader์—์„œ readLine().split(" ")์œผ๋กœ๋„ ๊ฐ€๋Šฅ
public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String line = bf.readLine();
    StringTokenizer st = new StringTokenizer(line, " ");
    int sum = 0;
    while(st.hasMoreTokens())
      sum += Integer.valueOf(st.nextToken());
    System.out.println(sum);
  }
}

 

StringBuilder

  • ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด ๋งŽ์€ ๊ฒฝ์šฐ ๋งค๋ฒˆ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค, ํ•œ ๋ฒˆ์— ๋ชจ์•„์„œ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด ์†๋„์ ์œผ๋กœ ์œ ๋ฆฌํ•œ๋ฐ, StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();

        for (int i = 1; i <= 5; i++) {
            sb.append(i + "\n");
        }

        System.out.println(sb);
    }
}

 

<์ฐธ๊ณ  ์ž๋ฃŒ>

 

[Algorithm] ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์œ„ํ•œ ์ž๋ฐ” Java IO

์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์œ„ํ•œ ์ž๋ฐ” IOcodeplus - ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋Œ€ํšŒ์—์„œ ์‚ฌ์šฉํ•˜๋Š” Java ์ฐธ๊ณ System.outSystem.out.println();System.out.printf("%d", n)์‹ค์ˆ˜ํ˜•, ๋ฌธ์žํ˜• ์ž๋ฃŒ ์ถœ๋ ฅ ๊ฐ€๋ŠฅScannernext[์ž๋ฃŒํ˜•]์„ ์ด์šฉํ•ด์„œ ์ž…๋ ฅ์„ ๋ฐ›์„ ์ˆ˜

ict-nroo.tistory.com

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ