在我们开始之前,我不认为这是一个重复的问题。我已阅读题为StringTokenzer countTokens() returns 1 with any string的问题,但这并没有解决正确定界的字符串被正确计数的事实,但正确定界的输入不是。countTokens()总是返回1与用户输入
当使用StringTokenizer类时,我发现countTokens方法返回不同的结果,具体取决于countTokens中的参数是定义的String还是用户定义的String。例如,下面的代码打印值4
String phrase = "Alpha bRaVo Charlie delta";
StringTokenizer token = new StringTokenizer(phrase);
//There's no need to specify the delimiter in the parameters, but I've tried
//both code examples with " " as the delimiter with identical results
int count = token.countTokens();
System.out.println(count);
但是当用户进入该代码将打印的值1:阿尔法BRAVO查理增量
Scanner in = new Scanner(System.in);
String phrase;
System.out.print("Enter a phrase: ");
phrase = in.next();
StringTokenizer token = new StringTokenizer(phrase);
int count = token.countTokens();
System.out.println(count);
Just throwong this out(from [docs](https://docs.oracle.com/javase/8/docs/api/java/util/StringTokenizer.html)):“* StringTokenizer是一个遗留类这是为了兼容性原因保留的,尽管**在新代码中不鼓励使用它***“。可能想考虑使用'String#split(String)' –
我很欣赏输入,但我需要为类Assignment使用StringTokenizer类。 –