【发布时间】:2013-10-21 15:28:06
【问题描述】:
我有以下代码
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line); // added to test if anything in String, and there is
int whiteSpace = 0;
int j = 0;
while (Character.isWhitespace(line.charAt(j))) {
whiteSpace++;
j++;
}
}
当我运行它时,我得到一个StringIndexOutOfBoundsException: String index out of range: 0,这对我来说毫无意义。我添加了一个System.out.print(line) 来测试字符串中是否有任何内容并且存在。我尝试用更高的值初始化j,仍然得到相同的东西,只是不同的String index out of range: ?。有谁知道我为什么会得到这个例外?对我来说毫无意义。
【问题讨论】: