Simplifying Java Software Development: How to Count the Number of Words in a String using Java (one-liner)

By Angsuman Chakraborty, Gaea News Network
Saturday, June 18, 2005

Java is a language of choice for millions of developers worldwide. In a series of articles I will show simple tips and techniques which make Java extremely powerful and yet simple to use. Today’s article is about using regex, a pattern matcher incorporated in Java (from 1.4 I believe).

Here is a sample code (line in bold) to count the number of words in any amount of text. The sample program counts the number of words in the argument to the program. The argument must be quoted to ensure separate words are clubbed together in a single sentence by the operating system.

public class WordCount {
  public static void main(String args[]) {
    System.out.println(java.util.regex.Pattern.compile("[\\w]+").split(args[0].trim()).length);
  }
}

Sample usage:
java WordCount “This is a sample phrase with 8 words”

All its does is compile the regular expression “\w” which matches with words and splits the phrase in an array of individual words. Then the total length of the array is printed.

Discussion

lei
March 20, 2010: 9:51 am

what will i do if the program ask was getting Background: N! (N factorial) can be quite irritating and difficult to compute for large values
of N. So instead of calculating N!, I want to know how many digits are in it. (Remember that
N! = N * (N - 1) * (N - 2) * … * 2 * 1)
Input: Each line of the input will have a single integer N on it 0 < N < 1000000 (1 million).
Input is terminated by end of file.
Output: For each value of N, print out how many digits are in N!.
Sample Input:
1
3
32000
1000000
Sample Output:
1
1
130271
5565709


sally
March 20, 2010: 2:33 am

how to write a java program that identifies how many “int”and “char”declaration are there in a given java program?


sally
March 20, 2010: 2:19 am

how to write a java program that identify how many “int” and “char”declaration are there in a given java program?


rajnish
June 6, 2009: 6:11 am

write a program to find largest word from given sentence..


Masila
August 1, 2007: 5:16 am

how to break the string value


mamduh
April 13, 2006: 3:10 pm

array prgam solution to count how many numbers are equals


mamduh
April 13, 2006: 3:08 pm

array to count how many numbers are eqauls


MuthamizhSelvan
December 8, 2005: 2:56 am

Dear Sir,
We are Stuckup in a code where we have to translate Indian rupees into words.
(e.g 1234.53–>?????)

June 19, 2005: 10:43 am

The regex is fine when you just deal with English, or other alphabetic languages, but when you’re dealing with Chinese or other Asian languages, your solution won’t work. Instead, you’d rather use Java’s locale specific String collators, in java.text.*. It’s much cleaner to use this approach, than looking for the simplest one-liner solution for English.


Val
June 19, 2005: 5:14 am

Even shorter:
int count = new StringTokenizer(args[0]).countTokens();

or

int count = args[0].trim().split(”\\s”).length;

June 19, 2005: 12:52 am

Thanks.

Can you please try it again and let me know if the problem is fixed on your end. You will have to refresh the feed. You can use any feed url.

I have tested here on two offline feed aggregators.
The problem was with Y!Q (Related Search) plugin.

Thanks again for the catch.

June 19, 2005: 12:29 am

Can you please tell me from when it started happening?

I saw the problem with the main feed and some other feeds like technology and web feeds.
However diabetes feed seems to be ok.

Looks like a problem with some characters in some posts. I am trying to find it. Debugging PHP is such a royal pain!

June 19, 2005: 12:22 am

Here is what I got:

Sponsor(s):
Tired of dealing with messy toner, lost fax pages and unreliable fax machines? Why pour more money into extra phone lines and machine maintenance when you can send and receive faxes by email? eFax is the #1 paperless faxing choice of individuals and businesses worldwide. Get a local fax number to send and receive faxes right from your email inbox. It’s the smart way to access and organize your faxed documents. Try eFax Plus free for a month.

You are on the phone with your client, going through an RFP that you emailed them. You hear them flip through the pages. Suddenly, they ask: “How much will this cost?” This happens every day, right? Not with GoToMeeting, the easiest way to hold online meetings. You can present, share, and field questions - right from your desk. So you stop the “skip ahead syndrome” before it starts. Try it FREE now.
© 2005 blog.taragana.com. This RSS Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@taragana.com so we can take legal action immediately.

June 19, 2005: 12:16 am

That’s is very surprising. Thanks for pointing it out. It was working well before. I tested it several times.
I am working to debug the problem. Can you tell me approximately from when the feed started spewing only ads?
BTW: I stopped the Ad engine for now, till I rectify the problem. However it seems the problem is elsewhere. The actual feed content is not getting posted in feed.

June 19, 2005: 12:02 am

Come on! There was absolutely no content in the feed. Not even a teaser. Just ads.

Can you point me to one of your feeds with some content (and preferably no ads), or would you rather me unsubscribe?

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :