본문 바로가기

JAVA/JAVA

자바 예제 - StringTokenizer : 문자열의 각 단어의 빈도수를 구하여 출력하는 프로그램을 작성(String.split)

반응형


<코드>


import java.util.StringTokenizer;


class StringTest06{

public static void main(String[] args){

String str = "hello hello hello java hello jsp hello orcale hello java";

String arr[] = new String[100]; //[hello] [java] [] [] [][]...[]

int cnt[] = new int[100];  //[3]  [1]  [] [] []..[]

int n=0;//중복되지 않는다면 n번째 데이터를 넣고 n을 증가시키고자 한다.  


int j=0;

String []st = str.split(" ");//[hello][hello][hello][java][hello][]..


for(int i=0; i<st.length; i++){

String s = st[i];


for(j=0; j<n; j++) //중복된 단어가 있는 검사.   j번째 중복된 위치

{

if(s.equals(arr[j])){

break;

}

}

if(j <n){

cnt[j]++;

}

if(n==0 || j == n){

arr[n] = s;

cnt[n] = 1;

n++;

}

}


for(int i=0; i<n; i++){

System.out.println(arr[i] + "==>" + cnt[i]);

}

}

}

반응형