2013-07-21 48 views
-4

我試圖在java中使用countTokens()方法,它僅顯示1作爲任何輸入的標記,作爲字符串給出的任何輸入。將任何字符串的標記計數顯示爲1

import java.util.*; 

public class uhu { 

    public static void main(String[] args) { 

    System.out.println("Hit n"); 
    Scanner sc = new Scanner(System.in); 
    try { 
     int n = sc.nextInt();//scan the size of the array 
     String[] str=new String[n]; 
     System.out.println("Enter elements"); 
     sc.nextLine(); 
     for (int i = 0; i < n; i++) //scanning the elements 
     { 
      str[i]=sc.nextLine(); 
     } 
     for (int i = 0; i < n; i++) //printing the count of tokens 
     { 
      StringTokenizer st=new StringTokenizer(str[i]); 
      System.out.println("Count of tokens for "+i+"string is :"+st.countTokens()); 
     } 
    } finally { 
     if (sc != null) 
      sc.close(); 
    } 

    } 

} 
+3

41分鐘前,您發佈了另一個問題,並要求提供具體信息,並提供輸入,預期輸出和實際輸出。對於這個問題以及將要問的所有未來問題也要這樣做。 –

回答

0

默認情況下StringTokenizer使用的分隔符列表空格字符,製表符,換行符,回車符和換頁符。

很可能您輸入的String不包含任何這些字符,導致令牌計數爲1

嘗試用空格分隔輸入String

相關問題