wood burning stoves 2.0*
The moose likes Java in General and the fly likes Find Word in String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Find Word in String" Watch "Find Word in String" New topic
Author

Find Word in String

Srinivas Katta
Ranch Hand

Joined: Feb 01, 2007
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

Joined: Jan 28, 2008
Posts: 5575

come up with some logic, that would be better.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sriram Naga
Greenhorn

Joined: Aug 24, 2010
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.

Try it and if you don't get the answer, would be happy to help you soon.

Others, please correct me if I am wrong.



[My Page] | [JavaRanch FAQ] | SCJP6

"I am not afraid of the storm, as I am learning to sail my ship"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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

Joined: Sep 08, 2010
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

Joined: Oct 13, 2005
Posts: 38509
    
  23
You may find this thread useful. But you probably won't
Joshua Barrett
Greenhorn

Joined: Sep 08, 2010
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

Joined: Oct 13, 2005
Posts: 38509
    
  23
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

Joined: Oct 13, 2005
Posts: 38509
    
  23
Sorry for the delay about the explanation about the editing. You would appear to have guessed the reason about spoon-feeding already.

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

Joined: Sep 08, 2010
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

Joined: Oct 13, 2005
Posts: 38509
    
  23
There would have been a slight enhancement possible, but what you posted was good code.
 
 
subject: Find Word in String