포스트

[Java] Baekjun No. 11021 - A+B - 7

I've outlined how to output multiple test cases in a formatted manner using BufferedReader and StringTokenizer in Java.

한국어 원문은 여기에서 볼 수 있습니다.
[Java] Baekjun No. 11021 - A+B - 7

BaekJoon 11021

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main(String args[]) throws IOException {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int num = Integer.parseInt(br.readLine());
        
        for(int i=0; i<num; i++) {
            
            StringTokenizer st = new StringTokenizer(br.readLine());
            
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            
            bw.write("Case #" + (i+1) + ": " + (a+b) + "\n");
        }
        
        bw.flush();
        bw.close();
    }
}

StringTokenizer location

  • Need to create a new line for each line
  • If you do not put it inside the loop, and only create it once outside the loop -> only the first line is read in the loop, and the tokens have already been used, so there is nothing to do in the next iteration.
  • Therefore, you need to put it inside a loop so that a new token can be read every time it repeats.

throws IOException

  • When a problem occurs while reading input, ex) no input, stream problem -> called IOException
  • If you use this kind of dangerous code -> you have to handle it yourself (try-catch) or throw it. throws IOException: If an error occurs, I pass it on without processing it! (commonly used in cote)