nextElement(): This method is available in java.util.StringTokenizer class of Java.
Syntax:
Object java.util.StringTokenizer.nextElement()
This method returns the nextToken of the String tokens.
Parameters: NA
Returns: the next token in the string.
Throws:
NoSuchElementException - if there are no more tokens in this tokenizer's string.
Approach 1: When no exception
Java
import java.util.StringTokenizer;public class StringTokenizernextElement {public static void main(String[] args) {StringTokenizer stringTokenizer =new StringTokenizer("Hello Java Program");System.out.println(stringTokenizer.nextElement());}}
Output:
Hello
Approach 2: NoSuchElementException
Java
import java.util.StringTokenizer;public class StringTokenizernextElement {public static void main(String[] args) {StringTokenizer stringTokenizer =new StringTokenizer("");System.out.println(stringTokenizer.nextElement());}}
Output:
Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.StringTokenizer.nextToken(StringTokenizer.java:348) at java.base/java.util.StringTokenizer.nextElement(StringTokenizer.java:406)
No comments:
Post a Comment