Tuesday, July 28, 2009

Length of a String - Java Program

Hi friends, welcome back to Java Code Online. Today's tutorial consists of a Java Program to find the number of characters in a String or number. This Java Program is helpful in finding the length of any String that is entered.

The Java Code begins by asking the user to enter any String or number, and then it displays the length of that entered String.

The Java Code is given below:-

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());
}
}


For more information on Java, and to get more of Java Code and Examples, keep buzzing Java Code Online.

No comments:

Post a Comment

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