Java StringTokenizer hasMoreElements() Method



Description

The Java StringTokenizer hasMoreElements() method is used to return the same value as the hasMoreTokens method. It exists so that this class can implement the Enumeration interface.

Declaration

Following is the declaration for java.util.StringTokenizer.hasMoreElements() method.

public boolean hasMoreElements()

Parameters

NA

Return Value

The method call returns 'true' if there are more tokens; false otherwise.

Exception

NA

Checking if StringTokenizer Has More Element Example

The following example shows the usage of Java StringTokenizer hasMoreElements() method to check if tokenizer has more elements or not. Here we're creating a StringTokenizer object using a given string. Then using hasMoreElements() method within while loop, we've checking if tokenizer has more elements and then printed the element.

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");

      // checking elements
      while (st.hasMoreElements()) {
         System.out.println("Next element : " + st.nextElement());    
      }
   }
}

Output

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

Next element : Come
Next element : to
Next element : learn

Checking if StringTokenizer with Delimiter Has More Element Example

The following example shows the usage of Java StringTokenizer hasMoreElements() method to check if tokenizer has more elements or not. Here we're creating a StringTokenizer object using a given string and a delimiter. Then using hasMoreElements() method within while loop, we've checking if tokenizer has more elements and then printed the element.

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","!");

      // checking elements
      while (st.hasMoreElements()) {
         System.out.println("Next element : " + st.nextElement());    
      }
   }
}

Output

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

Next element : Come
Next element : to
Next element : learn

Checking if StringTokenizer with Delimiter and Return Flag, Has More Element Example

The following example shows the usage of Java StringTokenizer hasMoreElements() method to check if tokenizer has more elements or not. Here we're creating a StringTokenizer object using a given string, a delimiter and returnDelim as true. Then using hasMoreElements() method within while loop, we've checking if tokenizer has more elements and then printed the element.

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

      // checking elements
      while (st.hasMoreElements()) {
         System.out.println("Next element : " + st.nextElement());    
      }
   }
}

Output

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

Next element : Come
Next element : !
Next element : to
Next element : !
Next element : learn
java_util_stringtokenizer.htm
Advertisements