java中的字符串索引越界错误(charAt)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22611268/
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
string index out of bounds error in java (charAt)
提问by Kyle
Quick question. I have this code in a program:
快速提问。我在一个程序中有这个代码:
input = JOptionPane.showInputDialog("Enter any word below")
int i = 0;
for (int j = 0; j <= input.length(); j++)
{
System.out.print(input.charAt(i));
System.out.print(" "); //don't ask about this.
i++;
}
- Input being user input
i
being integer with value of 0, as seen
- 输入是用户输入
i
为整数,值为 0,如所见
Running the code produces this error:
运行代码会产生这个错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(Unknown Source)
at program.main(program.java:15)
线程“main”中的异常 java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(Unknown Source)
at program.main(program.java:15)
If I change the charAt
int
to 0 instead of i
, it does not produce the error...
what can be done? What is the problem?
如果我将 更改charAt
int
为 0 而不是i
,它不会产生错误......
可以做什么?问题是什么?
采纳答案by Mena
Replace:
代替:
j <= input.length()
... with ...
... 和 ...
j < input.length()
Java String
character indexing is 0-based, so your loop termination condition should be at input
's length - 1.
JavaString
字符索引是基于 0 的,因此您的循环终止条件应该是 atinput
的长度 - 1。
Currently, when your loop reaches the penultimate iteration before termination, it references input
character at an index equal to input
's length, which throws the StringIndexOutOfBoundsException
(a RuntimeException
).
目前,当您的循环在终止前到达倒数第二次迭代时,它会input
在等于input
的长度的索引处引用字符,这会抛出StringIndexOutOfBoundsException
(a RuntimeException
)。
回答by Guillermo Merino
You were accessing the array from [0-length], you should do it from [0-(length-1)]
你从 [0-length] 访问数组,你应该从 [0-(length-1)]
int i = 0;
for (int j = 0; j < input.length(); j++)
{
System.out.print(input.charAt(i));
System.out.print(" "); //don't ask about this.
i++;
}
回答by Benjamin
Try the following:
请尝试以下操作:
j< input.length()
and then:
进而:
int i = 0;
for (int j = 0; j < input.length(); j++)
{
System.out.print(input.charAt(i));
System.out.print(" "); //don't ask about this.
i++;
}
回答by Hüseyin BABAL
Use this;
用这个;
for (int j = 0; j < input.length(); j++)
{
System.out.print(input.charAt(j));
System.out.print(" "); //don't ask about this.
}
回答by Bathsheba
String indexing in Java (like any other array-like structure) is zero-based. This means that input.charAt(0)
is the leftmost character. The last character is then at input.charAt(input.length() - 1)
.
Java 中的字符串索引(与任何其他类似数组的结构一样)是从零开始的。这意味着这input.charAt(0)
是最左边的字符。最后一个字符位于input.charAt(input.length() - 1)
。
So you are referencing one too many elements in your for
loop. Replace <=
with <
to fix. The alternative (<= input.length() - 1
) could bite you hard if you ever port your code to C++ (which has unsigned types).
因此,您在for
循环中引用了太多元素。替换<=
与<
要修复。<= input.length() - 1
如果您将代码移植到 C++(具有无符号类型),则替代 ( ) 可能会让您大吃一惊。
By the way, the Java runtime emits extremely helpful exceptions and error messages. Do learn to read and understand them.
顺便说一下,Java 运行时会发出非常有用的异常和错误消息。一定要学会阅读和理解它们。
回答by nikis
for (int j = 0; j < input.length(); j++)
{
System.out.print(input.charAt(j));
System.out.print(" "); //don't ask about this.
}
回答by Bhujang Bhagas
Replace for loop condition j <= input.length()
with j < input.length()
, as string in Java follows zero based indexing.
e.g. indexing for the String "india"
would start from 0 to 4.
更换for循环的条件j <= input.length()
有j < input.length()
,如字符串在Java中如下从零开始的索引。例如,字符串的索引"india"
将从 0 开始到 4。