Friday, August 14, 2009

Number of words in a String - Java Program

Hi friends, welcome back to Java Code Online. Today I will discuss a simple Java Program to find the number of words in any String entered by the user. The Java application starts by asking the user to enter a String, and then finds the number of words in that String.

The Java Code is displayed below for reference:-


public class NoOfWordsInString {

public static void main(String[] args) throws IOException {
System.out.println("Enter a string or number for which the number of words is to be counted: ");
InputStreamReader isr = new InputStreamReader(;
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
System.out.println("The no of words in the string are: "+str.length());

I hope the Java code above was helpful to you all, for more info on Java keep buzzing Java Code Online.

Related Java Programs
Reverse Java String
Length of a String

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.