我想分開一個長字符串到每個字,並按順序打印它們在Java中,但它會拋出異常StringIndexOutOfBounds
。繼承人的代碼,任何輸入的高度讚賞:屠宰一個字符串明智
public class SpellingChecker {
public static void test(String str) {
int i=0,j=0,n=str.length();
String temp="";
do{
for(i=j;str.charAt(i)!=' ';i++)
temp+=str.charAt(i);
temp+='\0';
System.out.println(temp);
temp="";
j=i+1;
}while(j<n);
}
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter string for which you want to check spelling : ");
String strng=input.next();
test(strng);
}
}
請問你能解釋一下嗎?在測試功能? – 2014-10-08 19:37:54
這段代碼正在做一些與預期不同的事 – 2014-10-08 19:43:01