【发布时间】:2014-10-08 19:35:28
【问题描述】:
我试图将一个长字符串分成每个单词并在 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);
}
}
【问题讨论】:
-
你能解释一下你在测试功能中的表现吗?
-
这段代码的作用与预期不同
标签: java string indexoutofboundsexception