[자바기초] split 함수로 단어 쪼갠후 특정단어에 대한 갯수출력



package cmd.ljh;

public class MainClass {

 //클래스에 있는 여러개의 문장을 받아서 쪼갠후 단어로 저장
 //저장된 특정단어에 대한 갯수 출력
 public static void main(String[] args){
  DataClass dc =  new DataClass();
  
  //this.words = "college, life, looking, death, company, never, one, years, now, just, love, everything, something, parents";
  String [] words = dc.words.split(", ");
  String [] temp = dc.txt.split(" ");
    
  //1번. 클래스 선언과 동시에 생성자에서 word를 넣어주는 방법
  WordCountClass []wcc = new WordCountClass[dc.words.length()];
  for (int i = 0; i < words.length; i++) {
   wcc[i] = new WordCountClass(words[i]);
  }
 
  //2번. 두줄로 하는 방법
//  WordCountClass []wcc = new WordCountClass[dc.words.length()];
//  for (int i = 0; i < words.length; i++) {
//   wcc[i] = new WordCountClass();
//   wcc[i].word = words[i];
//  }
//  
  for (int i = 0; i < words.length; i++) {
   for (int j = 0; j < temp.length; j++) {
    temp[j] = temp[j].toUpperCase();
    words[i] = words[i].toUpperCase();
    if (temp[j].contains(words[i])) {
     wcc[i].cnt++;
    }
   }
  }
  for (int i = 0; i < words.length; i++) {
   System.out.println(wcc[i].word + " " + wcc[i].cnt);
  }  
 } 
 
 public MainClass() {
 }
}

댓글