Forums Register Login

Finding out the most occuring word in a String?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
no wonder he is so sad, he hasn't seen this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 954 times.
Similar Threads
Apply function names as parameters in other functions
not leading to stack overflow error
how assignement takes place
x++ , ++x
nesting loops
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:57:29.