我尝试在 java 中使用 countTokens() 方法,对于我作为字符串给出的任何输入,它只显示 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();
}
}
}