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:-


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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(System.in);
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.