Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Find Word in String

Srinivas Katta
Ranch Hand
Posts: 76
Hi All,

In a Given String how can we find each word is repeated how many times?

For Ex: I have a String str = "One two one Three one two four five one two";

In the baove string I need to know that word one is repeated 4 times etc.

Thanks
Srinivas

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
come up with some logic, that would be better.

fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
The best way to figure out how to do it in Java is to figure out how you'd do it yourself. How would YOU find the number of times the word "one" appeared in your string? Write down step by step what you would do if you didn't have a computer.

What kinds of things would be helpful? Imagine if the string was so large, you couldn't do it in one sitting. You'd have to stop and come back several times. What kinds of things would you need to know or remember when you come back?

Sriram Naga
Greenhorn
Posts: 8
@Srinivas,

What Fred and Venkat has suggested might have suggested is correct and the right way. I assume if you were looking just for the code, you wouldn't have posted it here but googled it.

So let's see the approach.

Normally on paper, we would

1. Write each word, line by line.
2. If there is a repetition, increment the count next to the word.
3. At the end we would have the list of all words, with the respective number of occurrences to their right.

one two three two one one

one - 3
two - 2
three - 1

Think of a data structure which can solve this. Refer to Java Collections.

Also before insertion, you got to "break" the string, to have individual words.

Others, please correct me if I am wrong.

Campbell Ritchie
Sheriff
Posts: 48910
58
Srinivas Katta wrote: . . . For Ex: I have a String str = "One two one Three one two four five one two"; . . .
The word one appears thrice and the word One appears once. You will need to take case into consideration when you count words. There are methods in the String class which cope with case-insensitivity.

Joshua Barrett
Greenhorn
Posts: 27
Hi there.

Fresh brand new user here. I Figured I'll be seeking help here in the future, and might as well help out where I can.

That will turn your string into an array of individual words and make it possible to look at each word individually.

I then used a HashMap with the word as a key and the count of the words as a value.

Hope this helps.

Campbell Ritchie
Sheriff
Posts: 48910
58
You may find this thread useful. But you probably won't

Joshua Barrett
Greenhorn
Posts: 27
Campbell Ritchie wrote:You may find this thread useful. But you probably won't

I dont mind my post being edited, but would like to know why, especially if I did something wrong, so that it wont happen again.

If I had to guess it would that you guys like to assist rather than spoon feed to much...

Campbell Ritchie
Sheriff
Posts: 48910
58
Welcome to the Ranch Joshua Barrett

You will find this:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
at the top of the Beginning Java forum. That means it doesn't help anybody to give a direct answer like that. I am very sorry to do this to you when you are new, but I think Srinivas Katta will be better off without being given a straight answer. So I have pulled rank (please don't be annoyed with me) and deleted most of it.

I shall reinstate the useful hint, which I also made in the other thread about the similar question (see my earlier posting): use a Map and put the word in as the key and the count as the value.
It would appear from the existence of two threads about the same subject that there are two people trying to do the same task and both finding the same difficulty.

Campbell Ritchie
Sheriff
Posts: 48910
58

The bit about the other thread was actually intended for Srinivas Katta

Joshua Barrett
Greenhorn
Posts: 27
No worries.

Just put it down to me being too eager to please.

Although by you removing it I assume the code worked

I imagine HashMap was not the most optimal solution though.

Campbell Ritchie
Sheriff
Posts: 48910
58
There would have been a slight enhancement possible, but what you posted was good code.