當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java StringTokenizer hasMoreElements()用法及代碼示例


StringTokenizer類的hasMoreElements()方法還檢查此StringTokenizer是否還有其他可用的令牌。它類似於hasMoreTokens()。該方法專門存在,以便可以實現此類的Enumeration接口。

用法:

public boolean hasMoreElements()

參數:該方法不帶任何參數。


返回值:如果在當前位置之後在字符串中找到至少一個以上的令牌,則該方法返回boolean True。

下麵的程序說明StringTokenizer的hasMoreElements()方法的用法:
示例1:

// Java code to illustrate hasMoreElements() 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"); 
  
        // Counting the tokens 
        System.out.println("The number of Tokens are: "
                           + str_arr.countTokens()); 
  
        // Checking for any tokens 
        System.out.println(str_arr.hasMoreElements()); 
  
        // Checking and displaying the Tokens 
        while (str_arr.hasMoreElements()) { 
            System.out.println("The Next token: "
                               + str_arr.nextToken()); 
        } 
    } 
}
輸出:
The number of Tokens are: 4
true
The Next token: Lets
The Next token: practice
The Next token: at
The Next token: GeeksforGeeks

示例2:

// Java code to illustrate hasMoreElements() method 
  
import java.util.*; 
  
public class StringTokenizer_Demo { 
    public static void main(String args[]) 
    { 
        // Creating a StringTokenizer 
        StringTokenizer str_arr 
            = new StringTokenizer(""); 
  
        // Counting the tokens 
        System.out.println("The number of Tokens are: "
                           + str_arr.countTokens()); 
  
        // Checking for any tokens 
        System.out.println(str_arr.hasMoreElements()); 
    } 
}
輸出:
The number of Tokens are: 0
false


相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 StringTokenizer hasMoreElements() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。