一个小问题,,,麻烦看看,,
public class demo{
public static void main(String[] args)
{
String text="fang xing is no one";
int count=0;
int index=0;
do
{
++count;
++index;
index=text.indexOf(' ',index);
}
while(index !=-1);
System.out.println(index);
System.out.println(count);
index=0;
int endindex=0;
String[] array=new String[count];
for(int i=0;i<count;i++)
{
endindex=text.indexOf(' ',index);
if(index==-1)
array[i]=text.substring(index);
else
if(index !=-1)
array[i]=text.substring(index,endindex);
index=endindex+1;
System.out.println(array[i]);
}
// for(int i=0;i<5;i++)
// System.out.println(array[i]);
}
}
怎么不能输出:
fang
is
no
one
少一个one
运行后提示:
-1
5
fang
is
no
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: -17
at java.lang.String.substring(String.java:1768)
at demo.main(demo.java:27)
Press any key to continue...