1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* Java StringTokenizer return tokens as Objects example This example shows how to return tokens of java StringTokenizer as Objects instead of String. */ import java.util.StringTokenizer; public class StringTokenizerReturnTokensAsObjectsExample { public static void main(String[] args) { //create StringTokenizer object StringTokenizer st = new StringTokenizer("Java StringTokenizer Example"); /* To return tokens as Objects use hasMoreElements() method of StringTokenizer class which returns the same value as hasMoreTokens(), with nextElement() method which returns the same value as nextToken() method except that the return type of nextElement() method is Object and not a String. */ //iterate through tokens using hasMoreElements() method while(st.hasMoreElements()){ System.out.println(st.nextElement()); } } } /* Output Would be Java StringTokenizer Example */ |
Add Comment