File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Counting words in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Counting words in a String" Watch "Counting words in a String" New topic
Author

Counting words in a String

Mitch Kazlo
Greenhorn

Joined: Oct 30, 2009
Posts: 13
Hello,

I have to pass a string to a method and return the count of words within the String. The issue I'm having is how to ignore ALL whitespace while counting the words.
While doing some searching I see that most people are using a String array to count words so that is what I have written so far.

My questions are:

1) Can I do this without using an array and just using a for loop / if else combination?

2) How do I dump All of the whitespace. My array solution appears to work fine until the # of spaces between the words increases.

Thanks..

Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

On your original string, first call trim() to remove trailing/heading spaces, then you can use the replaceAll method on your trimmed string to replace multiple spaces into single spaces. Finally do the split the way you're currently doing.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mitch Kazlo wrote:
1) Can I do this without using an array and just using a for loop / if else combination?


What would be the advantage of that?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

it would probably make more sense to split your string using a regular expression...


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

Joined: Oct 30, 2009
Posts: 13
Thanks for all the help! Solved it with your advice Koen.

Jeff,

I was wondering if I could do it with a loop and if / else because we haven't been working with arrays,
but everything I came across on-line seemed to use them for this purpose.

-M
Mitch Kazlo
Greenhorn

Joined: Oct 30, 2009
Posts: 13
Woops... I spoke too soon. Thought I had it.. This is where I am. If I pass in a string with a lot of spaces this isn't working. This will return 14 instead of the correct work count 6.

Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Your regular expression is wrong. It only replaces 2 spaces by one; instead you want something more generic that replace ANY number of 1 or more spaces by just one space. Look into regular expression characters like ? and *

Also, you're not assigning the results of the replaceAll method to anything, for instance str = str.replaceAll(); You can also combine method calls on one line like this: String[] result = mystring.trim().replaceAll().split().
Mitch Kazlo
Greenhorn

Joined: Oct 30, 2009
Posts: 13
Thanks again.. I found the character I needed..

-M
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
fred rosenberger wrote:it would probably make more sense to split your string using a regular expression...


Yes, you could use a short hand character like \s and replace 1 or many space occurrences with just 1 space.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Koen Aerts wrote:On your original string, first call trim() to remove trailing/heading spaces, then you can use the replaceAll method on your trimmed string to replace multiple spaces into single spaces. Finally do the split the way you're currently doing.


Praveen Kumar M K wrote:
Yes, you could use a short hand character like \s and replace 1 or many space occurrences with just 1 space.


There's no need for any replaceAll() step. Just trim() then split() it all that's necessary. (Or, I suppose, you skip the trim() and do a single replaceAll() followed by split().)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Jeff Verdegan wrote:
There's no need for any replaceAll() step. Just trim() then split() it all that's necessary. (Or, I suppose, you skip the trim() and do a single replaceAll() followed by split().)


Since the OP doesn't care about the results of the split(), only about the count, there is no need for the trim() or the replaceAll().




Heck, using the same argument, there is a lot that can be done to optimize the regex too.



Henry
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Henry Wong wrote:
Since the OP doesn't care about the results of the split(), only about the count


Oops. Missed that part. Guess I need to learn to ReadTheQuestion.
 
GeeCON Prague 2014
 
subject: Counting words in a String