java.util.StringTokenizer.countTokens() Method

Advertisements


Description

The countTokens() method is used to calculate the number of times that this tokenizer's nextToken method can be called before it generates an exception.

Declaration

Following is the declaration for java.util.StringTokenizer.countTokens() method.

public int countTokens()

Parameters

  • NA

Return Value

The method call returns the number of tokens remaining in the string using the current delimiter set.

Exception

  • NA

Example

The following example shows the usage of java.util.StringTokenizer.countTokens()

package com.tutorialspoint;

import java.util.*;

public class StringTokenizerDemo {
   public static void main(String[] args) {
      // creating string tokenizer
      StringTokenizer st = new StringTokenizer("Tutorialspoint is the best site");
      
      // counting tokens
      System.out.println("Total tokens : " + st.countTokens());       
   }
}

Let us compile and run the above program, this will produce the following result.

Total tokens : 5



Advertisements
Advertisements