字符串索引越界异常java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20162145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:36:15  来源:igfitidea点击:

String index out of bounds exception java

javaexceptionsubstring

提问by Rakim

I am getting the following error when calling a function from within my class: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 Although I used a system prints to see the inputs I am passing in the substring() function and everything seems to be right. The function isContained() returns a boolean value defining whether the substring passed as a parameter is in a list of words. My code is:

从我的类中调用函数时,我收到以下错误: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 虽然我使用系统打印来查看我在 substring() 函数和所有内容中传递的输入似乎是对的。函数 isContained() 返回一个布尔值,定义作为参数传递的子字符串是否在单词列表中。我的代码是:

for(int i=0; i<=size; i++)
    for(int j=i+1; j<=size; j++)
        if(isContained(str.substring(i,j-i)))
            System.out.println(str.substring(i,j-i));

where size is the size of the string (str) I am passing in the function

其中 size 是我在函数中传递的字符串 (str) 的大小

采纳答案by Pshemo

You are calling str.substring(i, j-i)which means substring(beginIndex, endIndex), not substring(beginIndex, lengthOfNewString).

您正在调用str.substring(i, j-i)这意味着substring(beginIndex, endIndex), 不是substring(beginIndex, lengthOfNewString)

One of assumption of this method is that endIndexis greater or equal beginIndex, if not length of new index will be negative and its value will be thrown in StringIndexOutOfBoundsException.

这种方法的假设之一endIndex是大于或等于beginIndex,否则新索引的长度将为负数,其值将被抛出StringIndexOutOfBoundsException

Maybe you should change your method do something like str.substring(i, j)?

也许你应该改变你的方法做类似的事情str.substring(i, j)



Also if sizeis length of your strthen

另外如果size是你的str那么长度

for (int i = 0; i <= size; i++)

should probably be

应该是

for (int i = 0; i < size; i++)

回答by SASM

I think you need to change the looping condition which is the problem here. You are looping one more iteration when you do <=sizeand the index starts from i=0. You can change this

我认为您需要更改这里的问题所在的循环条件。当您这样做<=size并且索引从i=0. 你可以改变这个

for(int i=0; i<=size; i++)

to

for(int i=0; i<size; i++)

and also take care about the inner loop condition.

并注意内循环条件。

回答by Besoul

IndexOutOfBoundsException-- if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

IndexOutOfBoundsException-- 如果 beginIndex 为负数,或 endIndex 大于此 String 对象的长度,或 beginIndex 大于 endIndex。

Actually, your right edge of your substring function may be lower than the left one. For example, when i=(size-1)and j=size, you are going to compute substring(size-1, 1). This is the cause of you error.

实际上,您的子字符串函数的右边缘可能低于左边缘。例如,当i=(size-1)和 时j=size,您将计算substring(size-1, 1)。这是你错误的原因。