aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Finding out the most occuring word in a String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Finding out the most occuring word in a String?" Watch "Finding out the most occuring word in a String?" New topic
Author

Finding out the most occuring word in a String?

Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
Hi,
I use the Arrays.sort() method to sort the aray of Strings.
Then, I use a for loop and compare if the first string is equal to the second.
I get the logic, but I'm getting an array index out of bounds exception.
Please help.
Given a string "y z z x y z";
The desired output is ->"z" occurs 3 times

Kudos!
Faisal
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That would mean there's something wrong with your code. But nobody here can see the code (except you of course) so I'm not sure what kind of help you expected. Posting the code would make it easier for people to say things about that code.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Show us what you have done and we'll walk you from there....

Your question is not coherent with the approach you have done, if I'm not wrong you're trying to find out how many word x occur most in a String?

It's easy if you know the approach to solve it regardless whether you know Java or not. Search for similar questions already asked, there are
plenty of information that help you solve the problem.

I myself have posted a solution on similar topic, hope it help.


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Faisal syed wrote:Hi,
I use the Arrays.sort() method to sort the aray of Strings.
Then, I use a for loop and compare if the first string is equal to the second.
I get the logic, but I'm getting an array index out of bounds exception.
Please help.
Given a string "y z z x y z";
The desired output is ->"z" occurs 3 times

Kudos!
Faisal


Try doing it using HashMap if the data you have is not too large.


My Blog SCJP 5 SCWCD 5
 
 
subject: Finding out the most occuring word in a String?