[英]string index out of bounds error in java (charAt)
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++;
}
i
being integer with value of 0, as seeni
是值为 0 的整数Running the code produces this error:运行代码会产生这个错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:6
at java.lang.String.charAt(Unknown Source)在 java.lang.String.charAt(Unknown Source)
at program.main(program.java:15)在 program.main(program.java:15)
If I change the charAt
int
to 0 instead of i
, it does not produce the error...如果我将
charAt
int
更改为 0 而不是i
,则不会产生错误...
what can be done?可以做什么? What is the problem?
问题是什么?
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. Java
String
字符索引是基于 0 的,因此您的循环终止条件应为input
的长度 - 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
(一个RuntimeException
)。
String indexing in Java (like any other array-like structure) is zero-based . Java 中的字符串索引(与任何其他类似数组的结构一样)是从零开始的。 This means that
input.charAt(0)
is the leftmost character.这意味着
input.charAt(0)
是最左边的字符。 The last character is then at input.charAt(input.length() - 1)
.最后一个字符在
input.charAt(input.length() - 1)
。
So you are referencing one too many elements in your for
loop.因此,您在
for
循环中引用了太多元素。 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).如果您曾经将代码移植到 C++(具有无符号类型),则替代方案 (
<= input.length() - 1
) 可能会让您<= input.length() - 1
。
By the way, the Java runtime emits extremely helpful exceptions and error messages.顺便说一下,Java 运行时会发出非常有用的异常和错误消息。 Do learn to read and understand them.
一定要学会阅读和理解它们。
Replace for loop condition j <= input.length()
with j < input.length()
, as string in Java follows zero based indexing.将循环条件
j <= input.length()
替换为j < input.length()
,因为 Java 中的字符串遵循基于零的索引。 eg indexing for the String "india"
would start from 0 to 4.例如,字符串
"india"
索引将从 0 开始到 4。
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++;
}
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++;
}
Use this;用这个;
for (int j = 0; j < input.length(); j++)
{
System.out.print(input.charAt(j));
System.out.print(" "); //don't ask about this.
}
for (int j = 0; j < input.length(); j++)
{
System.out.print(input.charAt(j));
System.out.print(" "); //don't ask about this.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.