Home Java Example Java Util Breaking the String into Words



Breaking the String into Words
Posted on: April 16, 2007 at 12:00 AM
In this section, you will learn how to break the string into words.

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.

hasMoreTokens():
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.

nextToken():
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.io.*;
import java.util.*;

public class BreakStringToWord{
  String str;
  int count = 0;
  public static void main(String[] args) {
  BreakStringToWord c = new BreakStringToWord();
  }
  public BreakStringToWord(){
  try{
  InputStreamReader ir = new InputStreamReader(System.in);
  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()){
  count++;
  str = token.nextToken();
  System.out.println(str);
  }
  System.out.println("Number of Words : "+ count);
  }
  catch(IOException e){}
  }
}

Download this example.

Related Tags for Breaking the String into Words:
cstringideiocountusersedwordvinumberintthisidexampleprogramspacestobreakrambaseblankexamlanwordsbetweenespaceitdessectionlsbreaksusefromceinasmnttrtweenacebrokenbasedesaseintoproxaxampsspsoeeetwatpackishaivmplpreeaandstrrdstwssriringrdthsthatlsopleplprndonomogro


More Tutorials from this section

Ask Questions?    Discuss: Breaking the String into Words   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.