Java StringTokenizer nextElement() Method



Description

The Java StringTokenizer 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.

Getting Next Element of StringTokenizer Example

The following example shows the usage of Java StringTokenizer nextElement() method to get the next element of the tokenizer. Here we're creating a StringTokenizer object using a given string. Then using nextElement() method we've advanced by an element and then using nextElement() method again we've printed the next to next token.

package com.tutorialspoint;

import java.util.StringTokenizer;

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());
   }    
}

Output

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

Next element is : to

Getting Next Element of StringTokenizer with Delimiter Example

The following example shows the usage of Java StringTokenizer nextElement() method to get the next element of the tokenizer. Here we're creating a StringTokenizer object using a given string and a delimiter. Then using nextElement() method we've advanced by an element and then using nextElement() method again we've printed the next to next token.

package com.tutorialspoint;

import java.util.StringTokenizer;

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());
   }    
}

Output

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

Next element is : to

Getting Next Element of StringTokenizer with Delimiter Example

The following example shows the usage of Java StringTokenizer nextElement() method to get the next element of the tokenizer. Here we're creating a StringTokenizer object using a given string, a delimiter and returnDelim as true. Then using nextElement() method we've advanced by an element and then using nextElement() method again we've printed the next to next token.

package com.tutorialspoint;

import java.util.StringTokenizer;

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

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

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

Output

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

Next element is : !
java_util_stringtokenizer.htm
Advertisements