- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
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 : !
To Continue Learning Please Login
Login with Google