我正在制作一个程序,让用户输入一个句子,然后,应用程序会将字符串分解为子字符串,其中空格是原始字符串的分解。
import java.util.StringTokenizer;
public class whitespace {
public static void main(String[] args) {
String text = "supervisors signature tom hanks";
int tokenCount; //number of words
int idx=0; // index
String words[]=new String [500]; // space for words
StringTokenizer st=new StringTokenizer(text); // split text into segements
tokenCount=st.countTokens();
while (st.hasMoreTokens()) // is there stuff to get?
{
words[idx]=st.nextToken();
idx++;
}
}
到目前为止,我有这段代码,虽然它可以作为常规 Java 程序正常工作,但 while 循环似乎导致应用程序进入无限循环。有任何想法吗?