• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

StringTokenizer

May 25, 2011 By j2eereference Leave a Comment

Let us consider one example for splitting a complete string into different words.

import java.util.StringTokenizer;

 public class StringTockenizer {    

        public static void main(String[] args) {

        String str=””; 

        try{     

              str = “This is for learning StringTokenizer”;

              StringTokenizer token = new StringTokenizer(str);

              System.out.println(“Token count is “+token.countTokens());

              while (token.hasMoreTokens())

              {  

                    str = token.nextToken();

                    System.out.println(str);

              }       

        }

        catch(Exception e)

        {

              e.printStackTrace();

        }

 }

}

Output

Token count is 5 

This

is

for

learning

StringTokenizer

StringTokenizer constructor

StringTokenizer constructor  constructor creates token of the given string. It takes String as  value as a parameter which has to be tokenized.

 Important methods

1.      hasMoreTokens():

This method returns Boolean type value either true or false. If the above method returns true then the nextToken() method is called.

2.      nextToken():

This method returns the tokenized string which is nothing but separated word of the given string. 

3.      countTokens ();

This method return number of tokens in the input string.

Related Posts

  • Busy Spinning in mutithreading
  • Implementing queue in java
  • TreeSet vs ConcurrentSkipListSet
  • How to create immutable class?
  • Implementing Stack in java
  • What is ReentrantLock?
  • What is Semaphore in java
  • Why AtomicInteger class
  • What is CyclicBarrier?
  • CountDownLatch in java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.