Java 5일차 String 테스트2

/* String 클래스 안의 가장 많은 단어와 그 빈도수 세기 */

import java.util.Scanner;

public class StringTest2
{
public static void main(String[] args)
{
String lyrics = "Yellow diamonds in the light"+
"And we're standing side by side"+
"As your shadow crosses mine"+
"What it takes to come alive"+

"It's the way I'm feeling I just can't deny"+
"But I've gotta let it go"+

"We found love in a hopeless place"+
"We found love in a hopeless place"+
"We found love in a hopeless place"+
"We found love in a hopeless place"+

"Shine a light through an open door"+
"Love and life I will divide"+
"Turn away cause I need you more"+
"Feel the heartbeat in my mind"+

"It's the way I'm feeling I just can't deny";

String[] result = lyrics.split(" ");
int count_max=0, word_index=0;

for(int i=0; i<result.length; i++)
{
int count = 0;
for(int j=0; j<result.length; j++)
{
if(result[i].equals(result[j]))
count++;
}
if(count_max < count)
{
count_max = count;
word_index = i;
}
}

System.out.println(result[word_index]+" "+count_max);
}
}

댓글 없음: