Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A good way to count a specifed word?  RSS feed

 
Mike Cole
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

just a short question i have the problem that i have to count the occurencies of a word in a string.
Lets say the string is following:
"The quick, red blue green, Fox Rabbit, jumps around in my Fox-House and kills rabbits for fun. Green flies follow him."

And in such a text i have to find for example the words: fox, green and quick.
For each word that is found, i need to know how many times it is found.

Anyone has an easy solution for that?

Thnx

mk
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a perfect candidate for a Regular Expression. Have a look at the java.util.regex package.
 
Ruivaldo Neto
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the split method of the String class and so use the lenght one.

Cheers,
Rui
 
Bram Maes
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner class looks perfect to do this...
 
Mike Cole
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found a simple solution:


But i will still check the scanner class. The name sounds like fun
 
Mike Cole
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good feeling's gone...

What is wrong with this? Shouldn't that work?



Thnx for your help and time.

mk
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!