Monday, December 22, 2025
HomeLanguagesJavaStringTokenizer nextElement() Method in Java with Examples

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 not take any parameters.

Return Value: The method returns the next token present in the line of the string tokenizer.

Below programs illustrate the working of nextElement() Method of StringTokenizer:

Example 1:




// Java code to illustrate nextElement() method
  
import java.util.*;
  
public class StringTokenizer_Demo {
    public static void main(String args[])
    {
        // Creating a StringTokenizer
        StringTokenizer str_arr
            = new StringTokenizer(
                "Lets practice at Lazyroar");
  
        // Displaying the Tokens
        while (str_arr.hasMoreElements()) {
            System.out.println("The Next token: "
                               + str_arr.nextElement());
        }
    }
}


Output:

The Next token: Lets
The Next token: practice
The Next token: at
The Next token: Lazyroar

Example 2:




// Java code to illustrate nextElement() method
  
import java.util.*;
  
public class StringTokenizer_Demo {
    public static void main(String args[])
    {
        // Creating a StringTokenizer
        StringTokenizer str_arr
            = new StringTokenizer(
                "Welcome to Lazyroar");
  
        // Displaying the Tokens
        while (str_arr.hasMoreElements()) {
            System.out.println("The Next token: "
                               + str_arr.nextElement());
        }
    }
}


Output:

The Next token: Welcome
The Next token: to
The Next token: Lazyroar
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32455 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS