This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hi,
I have been trying to write the program which counts the most repetitive word in a String. And I am stuck. I tried Bubble sort, but didnt get it. Any ideas?
Agooner kumar wrote:Hi,
I have been trying to write the program which counts the most repetitive word in a String. And I am stuck. I tried Bubble sort, but didnt get it. Any ideas?
AG.
An idea - post the code that didn't work and indicate in what way it didn't work.
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Agooner kumar
Greenhorn
Joined: May 17, 2011
Posts: 4
posted
0
James Sabre wrote:
Agooner kumar wrote:Hi,
I have been trying to write the program which counts the most repetitive word in a String. And I am stuck. I tried Bubble sort, but didnt get it. Any ideas?
AG.
An idea - post the code that didn't work and indicate in what way it didn't work.
Oh well, thanks for the idea.. Here is the code.
String s ="My name is AG, My work is hilarious and ubiquitous.";
String s1[] = s.split(" ");
for(int i=0; i<s1.length; i++){
for(int j=i+1;j<s1.length; j++){
if(s1[j].compareTo(s1[i])==0){
System.out.println(s1[j]);
}
}
}
System.out.println(s1.length);
I have got the words, which are repetitive, but i want to find the count for the most repetitive word.
So keep track of the count for each word. A Map<String,Integer> is a usual choice, with the keys being the words and the values being the current count. You can use a HashMap for case sensitive matching, or a TreeMap combined with String.CASE_INSENSITIVE_ORDER for case insensitive matching.