본문 바로가기

알고리즘/JAVA

자바 공백포함 문자열 읽고 단어 단위로 자르기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;
import java.lang.*;
import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner sc = new Scanner(System.in);
        String s;
        s = sc.nextLine();
        
        StringTokenizer str = new StringTokenizer(s, " ");
        while (str.hasMoreTokens()) {
            System.out.println(str.nextToken());    
        }
        
        
    }
}

 

알고리즘 문제를 풀 때, 유용하게 사용.

 

 

 

문자열을 읽어들이고, 문자 단위로 출력.