简体   繁体   English

无法弄清楚为什么我得到了StringIndexOutOfBoundsException

[英]Can't figure out why I'm getting a StringIndexOutOfBoundsException

I have the following code 我有以下代码

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++;
    }
}

When I run it, I get a StringIndexOutOfBoundsException: String index out of range: 0 , which makes no sense to me. 当我运行它时,我得到StringIndexOutOfBoundsException: String index out of range: 0 ,这对我来说毫无意义。 I added a System.out.print(line) to test if there is anything in the String and there is. 我添加了System.out.print(line)来测试String中是否有任何东西。 I tried initializing j with higher values, still get same thing, just a different String index out of range: ? 我尝试使用较高的值初始化j ,但仍然得到相同的结果,只是另一个String index out of range: ? . Does anyone know why I'm getting this exception? 有谁知道我为什么要得到这个例外? Makes no sense to me. 对我来说毫无意义。

Try something more like this: 尝试更多类似这样的方法:

while(j<line.length) {
    if(Character.isWhitespace(line.charAt(j))) {
        whiteSpace++;
    }
    j++;
}

It's probably closer to what you're trying to do. 它可能更接近您要执行的操作。 What you have now, if it worked, would only increment j whenever there was a whiteSpace , and as such, even if you weren't getting the error, you'd be stuck in an infinite loop unless you had a String that consisted of only white spaces. 如果能正常工作,那么现在拥有的东西只会在存在whiteSpace时递增j ,因此,即使没有出现错误,也将陷入无限循环,除非您拥有包含以下内容的String :只有空格。 The above code will increment j on every character (so you check each character once and only once through the entire string), but increments whiteSpace only when Character.isWhitespace(line.charAt(j)) returns true . 上面的代码将增加j在每一个字符(所以你通过整个字符串一次且仅一次检查每一个角色),但增加whiteSpace只有当Character.isWhitespace(line.charAt(j))返回true

EDIT: Given the re-clarification of what you're actually trying to do, just drop the variable j as it muddies the readability (hence my confusion). 编辑:考虑到您实际要执行的操作,请重新添加变量j因为它会影响可读性(因此我感到困惑)。

while(whiteSpace<line.length && Character.isWhitespace(line.charAt(whiteSpace)))
    { whiteSpace++; }

I don't know what's in your line, but you should try : 我不知道您的路线是什么,但是您应该尝试:

while (j < line.length() && Character.isWhitespace(line.charAt(j))) {
    whiteSpace++;
    j++;
}

Maybe because you are incrementing j++ and then using it in : 可能是因为您要递增j ++,然后在其中使用它:

Character.isWhitespace(line.charAt(j))

If you only have one char on the line, which is a whitespace, then on the next iteration, it would try to reach next char, which doesn't exist. 如果在线上只有一个字符,即空格,那么在下一次迭代时,它将尝试到达不存在的下一个字符。

Hence you might have to check if char is not null, and then if it is a whitespace. 因此,您可能必须检查char是否不为null,然后检查它是否为空格。 Or brake if the next char is null. 如果下一个字符为null,则制动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道为什么我得到StackOverFlowError - I can't figure out why I'm getting StackOverFlowError 我在Ebay API上收到400错误的请求,我不知道为什么 - I'm getting a 400 Bad Request on the Ebay API and I can't figure out why 无法弄清楚为什么我得到了java.lang.NullPointerExceptio - Can't figure out why I'm getting a java.lang.NullPointerExceptio 无法弄清楚为什么我的方法异常 - Can't figure out why i'm getting an exception for my method 无法弄清楚为什么我得到空值 - Can't figure out why I am getting null values 我正在尝试合并两个排序的 arrays 但得到 java.lang.ArrayIndexOutOfBoundsException: 5 无法弄清楚为什么 - I'm trying to merge two sorted arrays but getting java.lang.ArrayIndexOutOfBoundsException: 5 can't figure out why 无法弄清楚为什么我在计算中得到空值 - Cannot figure out why i'm getting a null value in calculation Java:不断收到 IndexOutOfBoundsException,无法弄清楚我在哪里使用了无效索引 - Java: Keep getting an IndexOutOfBoundsException, can't figure out where I'm using an invalid index 为什么我得到 StringIndexOutOfBoundsException: String index out of range: 65 - Why am I getting StringIndexOutOfBoundsException: String index out of range: 65 无法弄清楚这个 java Exception: &quot;main&quot; java.lang.StringIndexOutOfBoundsException: String index out of range: 1 - Can't figure out this java Exception: "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM