StringTokenizer countTokens() Method in Java with Examples Last Updated : 28 Jan, 2019 Comments Improve Suggest changes Like Article Like Report The countTokens() method of StringTokenizer class calculate the number of times that this tokenizer's nextToken method can be called before the method generates any further exception. Note: The current position is not advanced during the process. Syntax: public int countTokens() Parameters: The method does not take any parameters. Return Value: The method is used to return the number of tokens remaining in the string using the current delimiter set. Below programs illustrate the working of countTokens() Method of StringTokenizer: Example 1: Java // Java code to illustrate countTokens() method import java.util.*; public class StringTokenizer_Demo1 { public static void main(String args[]) { // Creating a StringTokenizer StringTokenizer str_arr = new StringTokenizer( "Lets practice at GeeksforGeeks"); // Counting the tokens int count = str_arr.countTokens(); System.out.println("Total number of Tokens: " + count); // Print the tokens for (int i = 0; i < count; i++) System.out.println("token at [" + i + "] : " + str_arr.nextToken()); } } Output: Total number of Tokens: 4 token at [0] : Lets token at [1] : practice token at [2] : at token at [3] : GeeksforGeeks Example 2: Java // Java code to illustrate countTokens() method import java.util.*; public class StringTokenizer_Demo2 { public static void main(String args[]) { // Creating a StringTokenizer StringTokenizer str_arr = new StringTokenizer( "Welcome to GeeksforGeeks"); // Counting the tokens int count = str_arr.countTokens(); System.out.println("Total number of Tokens: " + count); // Print the tokens for (int i = 0; i < count; i++) System.out.println("token at [" + i + "] : " + str_arr.nextToken()); } } Output: Total number of Tokens: 3 token at [0] : Welcome token at [1] : to token at [2] : GeeksforGeeks Comment More infoAdvertise with us Next Article StringTokenizer countTokens() Method in Java with Examples C chinmoy lenka Follow Improve Article Tags : Misc Java Java - util package Java-Functions Java-StringTokenizer +1 More Practice Tags : JavaMisc Similar Reads StringTokenizer nextToken() Method in Java with Examples The nextToken() method of StringTokenizer class is used to return the next token one after another from this StringTokenizer. Syntax: public String nextToken() Parameters: The method does not take any parameters. Return Value: The method returns the next token present in the line of the string token 1 min read Stream count() method in Java with examples long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers 2 min read StringTokenizer nextElement() Method in Java with Examples The nextElement() method of StringTokenizer class is also used to return the next token one after another from this StringTokenizer. It is similar to the nextToken() method, except that the return type is Object rather than the String. Syntax: public Object nextElement() Parameters: The method does 2 min read StringBuffer codePointCount() method in Java with Examples The codePointCount() method of StringBuffer class is used to return the number of Unicode code points in the specified range of beginIndex to endIndex of String contained by StringBuffer. This method takes beginIndex and endIndex as a parameter where beginIndex is the index of the first character of 3 min read Java String contains() Method with Example The String.contains() method is used to search for a sequence of characters within a string. In this article, we will learn how to effectively use the string contains functionality in Java.Example:In this example, we check if a specific substring is present in the given string.Java// Java Program to 3 min read Like