https://www.acmicpc.net/problem/1152
띄어쓰기가 있는 문자열을 입력 받은 후, 단어의 개수를 구하는 문제.(공백으로 단어를 구분)
StringTokenizer에 있는 countTokens()를 활용하면 간단하게 풀 수 있다.
countTokens()는 nextToken메서드를 호출 할 수 있는 횟수를 계산하는 것이기 때문에 만약 "Hello My Name is Bonni"라는 문자열을 StringTokenizer롤 돌리면 5개의 토큰이 생성 된다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
// title : 단어의 개수
public class Q_1152 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
StringTokenizer st = new StringTokenizer(str);
System.out.println(st.countTokens());
}
}