hello,


hi can you help me what method to use to count how many words in a sentence...
hoping for your positive responds thank you in advance...

Use StringTokenizer in java.util.* to get each word as a token. The delimiters include space character, and other interpunction。

eg=
String s="Hai! Hello Java";//this is the input or sentence whose words are to be counted
int i,words=1;//variable words is used to count the number of words.
char c;//variable c is used to extract each character.
for(i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c==' ')//space is counted to determine the number of words.
words++;//increases the number of words by one.
}
System.out.println("no. of words ="+words);
}

hello,


hi can you help me what method to use to count how many words in a sentence...
hoping for your positive responds thank you in advance...

I just recently made a program just like this. tong1 was right; stringtokenizer can be used because it breaks the string up into tokens and you just need a variable of type integer to count the tokens. The method below is actually meant for something else but should work for your purpose.
Try something like this:

// Method to calculate the total of the numbers entered
	public static void getSum(String myString)
	{ 
		StringTokenizer sT = new StringTokenizer(myString);
		System.out.println("You entered a total of "+sT.countTokens()+" numbers");
        }

Hope this helps :P

Since the interpunction is not a word shoud we indicate some delimiters as well so that, e.g. "How are you ?" will be made of 3 words instead of 4.

StringTokenizer sT = new StringTokenizer(myString, " ,.!?'\"");
commented: Good :) +8

Since the interpunction is not a word shoud we indicate some delimiters as well so that, e.g. "How are you ?" will be made of 3 words instead of 4.

StringTokenizer sT = new StringTokenizer(myString, " ,.!?'\"");

Hello sir,

thank you for the reply and helping me and giving me idea, it works for me..please correct me if i am wrong with my code...thank you in advance hoping for your positive responds...

import java.util.*;


class Countingwords

{
	public static Scanner console = new Scanner(System.in); 
	public static void main(String []args)
	
	{
		String words;
		int count=0;
		
	    System.out.println("write a sentence");
	    words = console.nextLine();
	    
	    StringTokenizer input = new StringTokenizer(words,",.?!'\" ");
	    
	    while(input.hasMoreTokens())
	    {
	    
	    	count++;
	    	String w = input.nextToken();
	        System.out.println("The word " + w);
	    }
        System.out.println("The number of words in a sentence: " + count);
       
	}
	
}

eg=
String s="Hai! Hello Java";//this is the input or sentence whose words are to be counted
int i,words=1;//variable words is used to count the number of words.
char c;//variable c is used to extract each character.
for(i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c==' ')//space is counted to determine the number of words.
words++;//increases the number of words by one.
}
System.out.println("no. of words ="+words);
}

hello sir thank your for the reply i think it will have the problem if you determine by space, what if the user will input a sentence and he will add multiple spaces inbetween the words...please correct me if i am wrong....

Member Avatar for coil

What Megha did was to count the number of spaces, and assume the number of words was the number of spaces + 1.

Example:
Sentence: The quick dog jumped over the lazy fox

There are 7 spaces, so therefore there are 7+1=8 words. It works well for simple sentences with little/no punctuation.

What Megha did was to count the number of spaces, and assume the number of words was the number of spaces + 1.

Example:
Sentence: The quick dog jumped over the lazy fox

There are 7 spaces, so therefore there are 7+1=8 words. It works well for simple sentences with little/no punctuation.

hello sir thank you for the reply...

did you tried this sir?...The space space space quick space space space brown fox space jumped space space over the lazy dog.

how many words are there? i think it will not be 8 correct me if i am wrong...hoping for your positive responds....

Member Avatar for coil

Yes, I know, sentences like that would not work. Like I said, if the user enters only one space, then the above solution would work. Otherwise, use StringTokenizer, as has already been suggested.

Yes, I know, sentences like that would not work. Like I said, if the user enters only one space, then the above solution would work. Otherwise, use StringTokenizer, as has already been suggested.

thank you for the reply sir okay so that will not work in that kind of sentences...yes, i tried it stringtokenizer and it works for me,,sir can you look at my code if i get the right code?and please correct me if i am wrong...hoping for your positive responds...

Since the interpunction is not a word shoud we indicate some delimiters as well so that, e.g. "How are you ?" will be made of 3 words instead of 4.

StringTokenizer sT = new StringTokenizer(myString, " ,.!?'\"");

hi sir,


sir did i get it right?please correct my code...thank you in advance

Member Avatar for coil

I'm not sure what you mean by "right". If it compiles for you and it does what you want, then the code is correct...

I'm not sure what you mean by "right". If it compiles for you and it does what you want, then the code is correct...

thank you for the reply sir, yup it compile and it works...

eg=
String s="Hai! Hello Java";//this is the input or sentence whose words are to be counted
int i,words=1;//variable words is used to count the number of words.
char c;//variable c is used to extract each character.
for(i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c==' ')//space is counted to determine the number of words.
words++;//increases the number of words by one.
}
System.out.println("no. of words ="+words);
}

You're going to have to go back and fix this code.

What output should it give for the following strings?

This is a test.
This,string,has,five,words!


I think you'll find it's overcounting on the first and undercounting on the second.


EDIT: ...and I'm going to have to stop replying to threads before I've read all the posts. Doh!

You're going to have to go back and fix this code.

What output should it give for the following strings?

This is a test.
This,string,has,five,words!


I think you'll find it's overcounting on the first and undercounting on the second.


EDIT: ...and I'm going to have to stop replying to threads before I've read all the posts. Doh!

hello sir here is the outut sir..

This,string,has,five,words!
The word This
The word string
The word has
The word five
The word words
The number of words in a sentence: 5

Process completed.


is this correct sir?hoping for your positive responds...

Sorry jemz - that was a misfire on my part. I was commenting, redundantly, on megha's little code snip, which would have returned 1 as the number of words there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.