java.util.StringTokenizer.nextElement() Method



Description

The nextElement() method is used to return the same value as the nextToken method, except that its declared return value is Object rather than String.

Declaration

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

public Object nextElement()

Parameters

NA

Return Value

The method call returns the next token in the string.

Exception

NoSuchElementException − This is thrown if there are no more tokens in this tokenizer's string.

Example

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

package com.tutorialspoint;

import java.util.*;

public class StringTokenizerDemo {
   public static void main(String[] args) {
      
      // creating string tokenizer
      StringTokenizer st = new StringTokenizer("Come to learn");

      // moving to next element
      st.nextElement();

      // checking next to next element
      System.out.println("Next element is : " + st.nextElement());
   }    
}

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

Next element is : to
java_util_stringtokenizer.htm
Advertisements