import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int a = Integer.parseInt(br.readLine());
int b = Integer.parseInt(br.readLine());
System.out.println(a + b);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int i=0;i<T;i++) {
int A = sc.nextInt();
int B = sc.nextInt();
System.out.println(A+B);
}
sc.close();
}
}
한가지 의문은 왜 BufferedReader
랑 BufferedWriter
을 사용해서 했을 때는 이렇게 해도 저렇게 해도 자꾸 틀렸습니다😛
가 뜨는건지 모르겠다. 결국 Scanner
로 하니 되긴하는데.. 원인이 뭔지 모르겠다.
flush
close
빠뜨린거 하나 없는 것 같은데...
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()) {
int A = sc.nextInt();
int B = sc.nextInt();
System.out.println(A+B);
}
}
}
무작정 while(true)
로 실행해서 런타임 에러👊
를 한 대 얻어 맞고야 문제가 있다는 걸 깨닫는 코딩테스트 초보..
EOF(End of File)을 .hasNextInt()
로 해결했다.
EOF를 지정하는 또다른 방법 (https://st-lab.tistory.com/40)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
String str;
while( (str=br.readLine()) != null ){
st = new StringTokenizer(str," ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
sb.append(a+b).append("\n");
}
System.out.print(sb);
}
}
readLine()
이 null
이 아닌 경우를 계속 체크하면서 EOF를 지정한다.
StringTokenizer
를 통해 값을 split
해서 받아 연산하고, StringBuilder
에 넣어서 한번에 출력했다.