StringTokenizer

ims·2021년 1월 15일
0

알고리즘

목록 보기
18/23

StringTokenizer

public class StringTokenizerTest {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String s = br.readLine();
        StringTokenizer st = new StringTokenizer(s,"+",true);

        List<String> list = new ArrayList<>();

        System.out.println(st.countTokens());

        while(st.hasMoreTokens()){
            list.add(st.nextToken());
        }

        for (String s1 : list) {
            System.out.println(s1);
        }
    }
}

위의 코드 참고

? 부분

public class StringTokenizerTest {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String s = br.readLine();
        StringTokenizer st = new StringTokenizer(s,"+",true);

        List<String> list = new ArrayList<>();

        for(int i=0;i<st.countTokens();i++){
            list.add(st.nextToken());
        }
        for (String s1 : list) {
            System.out.println(s1);
        }
    }
}

위와 같이 넣으면 앞의 몇개만 값이 들어가고, 뒤에 있는 값들은 들어가지 않는다. 이유는 잘 모르겠다.

profile
티스토리로 이사했습니다! https://imsfromseoul.tistory.com/ + https://camel-man-ims.tistory.com/

0개의 댓글