Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Breaking the String into Words 
 

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.

                         

» View all related tutorials
Related Tags: c string ide class reference io references size sed get vi key value field fields this id length max preferences

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

how are we break a string in to word and these string match with datbase
please send me with code

Posted by kapiltyagi on Wednesday, 03.5.08 @ 10:55am | #51391

can i ask aomething?
:how to break words without using a tokenizer method?..

Posted by joanafe on Thursday, 01.24.08 @ 10:43am | #45983

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.