This week's book giveaway is in the Agile/Processes forum.
We're giving away four copies of Building Green Software: A Sustainable Approach to Software Development and Operations and have Anne Currie, Sarah Hsu , Sara Bergman on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

how to count words in a string

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have to initiate the process. so far what you have done?

hint: parse and then count!
 
praneeth ram
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I done it successfully, here is the solution..

 
Ranch Hand
Posts: 206
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.




 
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see two words "<mobile>" in his original base string. You see more than that?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zandis Murāns wrote:



I'm surprised that anyone would rather write such confusing code instead of
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what was the question about? Was it about words or tags?
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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? :)
reply
    Bookmark Topic Watch Topic
  • New Topic