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.
The moose likes Java in General and the fly likes String program to count the repetitive words in a string. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "String program to count the repetitive words in a string." Watch "String program to count the repetitive words in a string." New topic
Author

String program to count the repetitive words in a string.

Agooner kumar
Greenhorn

Joined: May 17, 2011
Posts: 4
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?


Thanks,
AG.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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
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.

Thanks,
AG
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: String program to count the repetitive words in a string.
 
Similar Threads
Scissors-rock-paper game looking for better way to write the program
Equivalent of Entity
Book is real Help full!
Passing Externally values
WA #2 ..... word association