File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to count words in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to count words in a string" Watch "how to count words in a string" New topic
Author

how to count words in a string

praneeth ram
Ranch Hand

Joined: Sep 21, 2009
Posts: 60
hi,

i want to count a word in a particular string my string is like bellow.



From the above String i want to know the count of <mobile>. how can we achieve it.

Thanks,
Praneeth.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

you have to initiate the process. so far what you have done?

hint: parse and then count!
praneeth ram
Ranch Hand

Joined: Sep 21, 2009
Posts: 60
I done it successfully, here is the solution..

Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
praneeth ram wrote:I done it successfully, here is the solution..




First of all I'm not sure why you're using "indexOf()" method ? indexOf is get the location of an object in a List, it doesn't do any searching for you.


The result based on the code you provided is: 17

It's not a word count number that you're getting, 17 is a number of location where <mobile> tag started in the "base" String. You can check and start counting
from the beginning of the String at 0 to 17, 17 where the tag <mobile> located.

This type of problem is not difficult to solve if you know how by identify the problem and analyze it, do a research, and find how what approaches in Java
that you need to utilize to solve the problem.


Tips:

Identify The Problems: Counts words when encounter pattern "<mobile>" in "base" String.

Analyze: What words do you need to count? Is it any word or character between <mobile> tag?

Research: After you have analyzed and understand about the problem next step is to do the research
about how to solve the problem. Gather all the information you need that help solve the problem.

Implementation: Find out what approaches in Java that you can utilize to solve this problem. This is a
final step to put your Java knowledge in use in the real world problem.


Hints:

To count words in the String variable "base" or in a file or input you can do the following. You need to do
some coding and test in order to find out what's the best approach for this type of problem.

1. First you need to read in the String.
2. Check the entire String for the tag <mobile>.
3. If it found the tag <mobile>, count word or character in the tag.

How do you implement that ? Well, I let the rest to you to solve it. Show us what you have done and I'll follow up
with suggestions.

Without any prior knowledge or experience you're able to solve the problem based on the approaches I stated above.

I can give you a hint utilize Scanner, Regex Expression, and other approaches.

Your code won't work, you need to start from SCRATCH based on Hints & Tips I provided above.





OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Why not leverage the power of Regex? We can end up with more readable and maintainable code:

[Edit: Whoops, I like Tommy's answer better. Code snippet removed]

Hint: use Regex to create a pattern and then something that can find a match for that pattern in your String.
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

I think there is absolutely no need for using regex in such case.
Right in the opposite, i think he MUST user power of indexOf() functions:
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Zandis Murāns wrote:I think there is absolutely no need for using regex in such case.
Right in the opposite, i think he MUST user power of indexOf() functions:



Your code totally FAIL! It only works word with regular word without tag. If you put or replace with the original "base" String you'll see why, you code only count 2 hence two <mobile> tags are in the String.

Try to solve the problem based on the original information.

Try this:




Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

I see two words "<mobile>" in his original base string. You see more than that?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Zandis Murāns wrote:


I'm surprised that anyone would rather write such confusing code instead of
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Zandis Murāns wrote:I see two words "<mobile>" in his original base string. You see more than that?


Yes, it's 2 words <mobile>, your code works if you or praneeth ram only wants to count <mobile>. My assumption is step further, count word when encounter <mobile> tag in a String. That's a totally different story then....

Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

So what was the question about? Was it about words or tags?
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Zandis Murāns wrote:So what was the question about? Was it about words or tags?


If so, only count for <mobile> then utilize Regex is is more efficient and flexible like Dennis has proposed. With Regex you
can search in any pattern you like and whatever you like.

The indexOf() method place a huge limitation on searching. Let's say if you want to search for <mobile> and <number> or other specific character or pattern, white space, digit, sign etc include and exlude in the String? Of course indexOf() can't achieve this beside return an index location...


With Regex you can produce the same result as indexOf(), but Regex is more efficient and flexible for String search. It's simple and straight forward...

Try this: Alter your search expression with different expression pattern and see how efficient and flexible Regex is.

Hints:
pattern:
\w
[abc] Searches only for a's, b's or c's
[a-f] Searches only for a, b, c, d, e, or f characters
[a-fA-F] Searches for the first six letters of the alphabet, both cases.

Searches Using Quantifiers etc...




Output: 17 79 Word count: 2


Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

You're not getting my point. I know regex very well and I know regex's possibilities very well neither.
I see simple case, I see as simple solution as the case was.
Anyways, you can continue argue on why solution of mine was good or not, in meantime I'll continue to think about efficiency of the same code of mine and go further. So good luck on that.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Zandis Murāns wrote:You're not getting my point. I know regex very well and I know regex's possibilities very well neither.
I see simple case, I see as simple solution as the case was.
Anyways, you can continue argue on why solution of mine was good or not, in meantime I'll continue to think about efficiency of the same code of mine and go further. So good luck on that.


Sure, I don't getting your point why you're using indexOf() instead Regex.

Can you achieve with indexOf() with search pattern "[k-m]" ? Count how many letter in the String range from k-m, K-M, A-C, etc. ? NO of course, beside one match for a specific word or character an indexOf() can achieve.

Can you count how many digits 8 or 0, 0-8, 3-9 in the string with indexOf()?? Absolutely NO right??

With Regex you CAN:

Example,

Pattern p = Pattern.compile("[0-9]");





Where do you see the efficiency of indexOf() and go further? Could you dialogue in coding so we can see? :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to count words in a string