• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding out the most occuring word in a String?

 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic