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:
import java.util.*;
public class StringTokenizer_Demo {
public static void main(String args[])
{
StringTokenizer str_arr
= new StringTokenizer(
"Lets practice at GeeksforGeeks" );
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: GeeksforGeeks
Example 2:
import java.util.*;
public class StringTokenizer_Demo {
public static void main(String args[])
{
StringTokenizer str_arr
= new StringTokenizer(
"Welcome to GeeksforGeeks" );
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: GeeksforGeeks
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Jan, 2019
Like Article
Save Article