Método StringTokenizer nextElement() en Java con ejemplos

El método nextElement() de la clase StringTokenizer también se usa para devolver el siguiente token uno tras otro de este StringTokenizer. Es similar al método nextToken(), excepto que el tipo de devolución es Object en lugar de String.

Sintaxis:

public Object nextElement()

Parámetros: El método no toma ningún parámetro.

Valor de retorno: el método devuelve el siguiente token presente en la línea del tokenizador de string.

Los siguientes programas ilustran el funcionamiento del método nextElement() de StringTokenizer:

Ejemplo 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 GeeksforGeeks");
  
        // Displaying the Tokens
        while (str_arr.hasMoreElements()) {
            System.out.println("The Next token: "
                               + str_arr.nextElement());
        }
    }
}
Producción:

The Next token: Lets
The Next token: practice
The Next token: at
The Next token: GeeksforGeeks

Ejemplo 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 GeeksforGeeks");
  
        // Displaying the Tokens
        while (str_arr.hasMoreElements()) {
            System.out.println("The Next token: "
                               + str_arr.nextElement());
        }
    }
}
Producción:

The Next token: Welcome
The Next token: to
The Next token: GeeksforGeeks

Publicación traducida automáticamente

Artículo escrito por Chinmoy Lenka y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *