Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Breaking the String into Words

In this section, you will learn how to break the string into words.


How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value

Breaking the String into Words


In this section, you will learn how to break the string into words. The java.util.*; package provides you a simple method to breaking the string into separate words.

This program takes a string from user and breaks it into words. The given string has been broken into words based on the blank spaces between the words. This program also counts the number of words present in the string. Following are some methods and APIs which have been used in the program:

StringTokenizer(String str):
Above constructor of the StringTokenizer class of the java.util.* package. This constructor creates token of the given string. It takes a string type value as a parameter which has to be tokenized. The passed string is the collection of multiple sets like: blank space (" "), Tab character ("\t"), new line character ("\n") etc.

Above method checks the token created by the StringTokenizer() methods. It returns Boolean type value either true or false. If the above method returns true then the nextToken() method is called.

This method returns the tokenized string which is the separate word of the given string. Through the help of the available spaces this method distinguish between words.

Here is the code of program:

import java.util.*;

public class BreakStringToWord{
  String str;
  int count = 0;
  public static void main(String[] args) {
  BreakStringToWord c = new BreakStringToWord();
  public BreakStringToWord(){
  InputStreamReader ir = new InputStreamReader(;
  BufferedReader br = new BufferedReader(ir);
  System.out.print("Enter String: ");
  str = br.readLine();
  System.out.println("Enter String is : "+ str);
  System.out.println("Breaking this string is : ");
  StringTokenizer token = new StringTokenizer(str);
  while (token.hasMoreTokens()){
  str = token.nextToken();
  System.out.println("Number of Words : "+ count);
  catch(IOException e){}

Download this example.


Share on Google+Share on Google+

Breaking the String into Words

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Breaking the String into Words   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 18, 2011
number count project with netbense

how to made this project
February 11, 2012

I need the java code for stop words removal in multi-document summarization........pls help me
February 11, 2012

i need the java code to remove stop-words in multi-document summarization