[자바기초] 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() {
 }
}

댓글

이 블로그의 인기 게시물

[자바기초] jxl을 이용하여 자바에서 엑셀파일 읽고,쓰기

[자바기초] Vector, Iterator를 이용해서 정수 삽입후 모든 정수 출력 및 합산

[자바기초] HashMap으로 [학생 이름, Student 객체]를 이용하여 저장, 출력