Java 字符串索引超出范围异常,字符串索引超出范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28777630/
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 bound exception, String index out of range
提问by Arvind Lakhani
So, I was writing a simple program to enter a string and count the total no. of m. So, here's my code
所以,我正在编写一个简单的程序来输入一个字符串并计算总数。米。所以,这是我的代码
for(int i=0; i<=n; i++)
{
if((str.charAt(i)=='m'))
{
} else {
count++;
}
}
System.out.println("The total number of m is "+count);
where n=str.length();
and str is a string which I had taken but there this error which keeps coming
wheren=str.length();
和 str 是我使用的字符串,但是这个错误不断出现
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 14
at java.lang.String.charAt(String.java:646)
at javaapplication.JavaApplication.main(JavaApplication.java:28
Java Result: 1
what's this error and how to remove it?
这是什么错误以及如何删除它?
采纳答案by Eran
A String of length() == n
has valid indices from 0 to n-1;
的字符串length() == n
具有从 0 到 n-1 的有效索引;
Change
改变
for(int i=0; i<=n; i++)
to
到
for(int i=0; i<n; i++)
回答by Amanda Kirk
Remember Strings are indexed from 0 to StringName.length() - 1. Since you are iterating through StringName().length -- you are actually going right outside the "bounds" of the string which is causing the error. You need to make sure your indices are correct in your for loop.
请记住,字符串的索引从 0 到 StringName.length() - 1。由于您正在迭代 StringName().length - 您实际上是在导致错误的字符串的“边界”之外。你需要确保你的索引在你的 for 循环中是正确的。
回答by Nin-ya
Characters in String variable start at 0 index.
String 变量中的字符从 0 索引开始。
Also if you want to count the total appearance of small letter m
, move your count++
to your if block statement
.
另外如果你想计算小写字母的总出现次数m
,把你的移动count++
到你的if block statement
。
n=str.length() - 1;
for(int i=0; i<=n; i++)
{
if((str.charAt(i)=='m'))
{
count++;
}
}
System.out.println("The total number of m is "+count);
回答by user3437460
Imagine you have the following array of length 7:
假设您有以下长度为 7 的数组:
-----------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | <-- Array index
-----------------------------
|10 |20 |30 |40 |50 |60 |70 | <-- Array values
-----------------------------
A for loop of for(int i=0; i<=n; i++)
in this case will loop 8 timesiterating from index 0 to 7.
for(int i=0; i<=n; i++)
在这种情况下,for 循环将从索引0 到 7循环8 次。
But the array element at index 7does not exist, hence giving outOfBoundsException
.
但是索引7处的数组元素不存在,因此给出outOfBoundsException
.
Where as a for loop of for(int i=0; i<n; i++)
will loop 7 timesiterating from 0 to 6.
其中 for 循环for(int i=0; i<n; i++)
将循环7 次,从0 到 6迭代。
回答by MaxDevelop
charat works with index(works from to n-1), but in your for you get to condition where you have i=n, in this case charat throws exception because it does not have that index in array
charat 与索引一起工作(从 n-1 开始工作),但在你的条件下,你有 i=n,在这种情况下,charat 抛出异常,因为它在数组中没有该索引