곽로그
[백준 10820, Java] 문자열 분석 본문
문제
10820번: 문자열 분석
문자열 N개가 주어진다. 이때, 문자열에 포함되어 있는 소문자, 대문자, 숫자, 공백의 개수를 구하는 프로그램을 작성하시오. 각 문자열은 알파벳 소문자, 대문자, 숫자, 공백으로만 이루어져 있
www.acmicpc.net
check
1. 입력의 끝
- (line =br.readLine())!=null 이면 입력의 끝이고, 따라서 종료가 되어야 한다고 생각했는데, 콘솔창에서는 계속 입력을 받았다. 구글링을 해보니 아래와 같은 stackoverflow를 찾았다.
stackoverflow.com/questions/14581205/bufferedreader-readline-waits-for-input-from-console
BufferedReader.readLine() waits for input from console
I am trying to read lines of text from the console. The number of lines is not known in advance. The BufferedReader.readLine() method reads a line but after the last line it waits for input from the
stackoverflow.com
요는 콘솔에서 BufferedReader가 콘솔입력의 마지막임을 알 수 있게 해야한다는 것이다.
algospot.com :: 질문과 답변: 자바 입력시 마지막줄
자바 입력시 마지막줄 5개의 댓글이 있습니다.
algospot.com
기본적인 입출력 - stream, stdin, stdout, EOF(End of File)
* 스트림(Stream) - C 프로그램은 파일이나 콘솔의 입출력을 직접 다루지 않고, 스트림이라는 것을 통해 다룬다. - 스트림은 실제 입력이나 출력이 표현된 데이터의 이상화된 흐름이다. - 즉, 스트림
keepdev.tistory.com
표준 입출력에서 EOF를 발생시키기 위해서는 모든 입력을 마치고 ctrl+d (윈도우기준)을 누르면 된다.
소스
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int countOfLowerCase;
int countOfUppserCase;
int countOfBlank;
int countOfNum;
String word;
while ((word = br.readLine()) != null) {
countOfLowerCase = 0;
countOfUppserCase = 0;
countOfBlank = 0;
countOfNum = 0;
for (int index = 0; index < word.length(); index++) {
char alphabet = word.charAt(index);
if (Character.isUpperCase(alphabet)) {
++countOfUppserCase;
} else if (Character.isLowerCase(alphabet)) {
++countOfLowerCase;
} else if (alphabet == ' ') {
++countOfBlank;
} else {
++countOfNum;
}
}
// System.out.println(countOfLowerCase+" "+countOfUppserCase+" "+countOfNum+" "+ countOfBlank);
bw.write(String.valueOf(countOfLowerCase) + " " + String.valueOf(countOfUppserCase) + " " + String.valueOf(countOfNum) + " " + String.valueOf(countOfBlank));
bw.write("\n");
bw.flush();
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[백준 17413, Java] 단어 뒤집기2 (0) | 2020.10.28 |
---|---|
[백준 1406, Java] 에디터 (0) | 2020.10.28 |
[백준 1935, Java] 후위표기식2 (0) | 2020.10.26 |
[백준1158, Java] 요세푸스 문제 (0) | 2020.10.20 |
백준 19236 청소년 상어 (Java) (0) | 2020.10.17 |