我正在制作一个程序,让用户输入一个句子,然后,应用程序会将字符串分解为子字符串,其中空格是原始字符串的起始位置。将字符串转换为子字符串Android
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循环似乎导致应用程序进入一个无限循环。有任何想法吗?
+1
你应该用字符串arraylist替换你的字符串数组。创建500个字符串的数组效率不高。另一方面,Arraylists是动态的,可以增长以适应您的需求。 –
2012-03-06 12:33:17