Course Curriculum

StringTokenizer methods in Java

StringTokenizer methods in Java

  •             StringTokenizer methods in Java with example :

        /                      /                   |                                              
hasMoreToken  nextToken   countTokens nextElement hasMoreElements

Following are the StringTokenizer class methods :

  • hasMoreTokens(): The method java.util.StringTokenizer.hasMoreTokens() plays role in testing, if tokens are present for the StringTokenizer’s string.
    Those characters that are considered to be delimiters by the StringTokenizer object are changed to characters in the string delimiter. Then the next token to the current position in the string is returned.
    Syntax:
    public boolean hasMoreTokens()
    Returns: True if and only if next token to the current
    position in the string exists, else false.
  • nextToken(): The method java.util.StringTokenizer.nextToken() returns the next token from the given StringTokenizer.
    Syntax:
    public String nextToken()
    Return: the next token from the given StringTokenizer
    if present.
    Throws: NoSuchElementException - if no more token are left.
  • countTokens(): The method java.util.StringTokenizer.countTokens() returns total number of tokens present so that we can use nextToken() method before it gives an exception..
    Syntax:

public int countTokens()
Return : the number of tokens remaining in the
string using the current delimiter set.

// Program in Java illustrates the methods of StringTokenizer class:
// hasMoreToken nextToken countTokens
import java.util.*;
public class NewClass
{
public static void main(String args[])
{
String mydelim = " : ";
String mystr = "JAVA : Code : String : Tokenizer : Prutor";

// Use of Constructor 2
// Here we are passing Delimiter - "mydelim"
StringTokenizer prutor3 =
new StringTokenizer(mystr, mydelim);

// Printing count of tokens and tokens
int count = prutor3.countTokens();
System.out.println("Number of tokens : " + count + "n");
for (int i = 0; i <count; i++)
System.out.println("token at [" + i + "] : "
+ prutor3.nextToken());

// .hasMoreTokens() method checks for more Tokens.
// Here not working as no Tokens left
while (prutor3.hasMoreTokens())

// .nextToken is method is returning next token.
System.out.println(prutor3.nextToken());
}
}
Output:

Number of tokens : 5
token at [0] : JAVA
token at [1] : Code
token at [2] : String
token at [3] : Tokenizer
token at [4] : Prutor

  • nextElement(): The method java.util.StringTokenizer.nextElements() works similar to nextToken except that it returns an Object rather than String.
    Exists so that this class can implement the Enumeration interface.
    Syntax:
    public Object nextElement()
    Return: the next token from the given StringTokenizer.
    Throws:NoSuchElementException - if there are no more tokens left.
  • hasMoreElements(): This method java.util.StringTokenizer.hasMoreElements() returns the same value as hasMoreToken. It exists so that the class can implement the Enumeration interface.
    Syntax:
    public boolean hasMoreElements()
    Return: true if tokens are present in the string, else false

// Program in Java illustrates the methods of StringTokenizer
// class: hasMoreElements, nextElement and nextElement
import java.util.*;
public class NewClass
{
public static void main(String args[])
{
String mydelim = " : ";
String mystr = "JAVA : Code : String : Tokenizer : Prutor";

// Use of Constructor 2
// Here we are passing Delimiter - "mydelim"
StringTokenizer prutor =
new StringTokenizer(mystr, mydelim);

// .countTokens() method counts no. of tokens present.
int count = prutor.countTokens();
System.out.println("Number of tokens : " + count);

// use of hasMoreElements() - true if tokens are present
while (prutor.hasMoreElements())

// use of nextElement() - returns the next token
System.out.println(prutor.nextElement());
}
}
Output:

Number of tokens : 5
JAVA
Code
String
Tokenizer
Prutor
Important Points:

  • countTokens() method is a good alternative in using the combination hasMoreTokens and nextToken().
  • The combination of countTokens and nextToken is used if you are interested in the number of tokens also.
(Next Lesson) How to start learning Java