字符串索引超出绑定异常,字符串索引超出范围 [英] string index out of bound exception, String index out of range
本文介绍了字符串索引超出绑定异常,字符串索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我正在编写一个简单的程序来输入字符串并计算总数。米
所以,这是我的代码
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);
其中 n = str.length();
和str是我已经采取的字符串,但是这个错误一直存在
where n=str.length();
and str is a string which I had taken but there this error which keeps coming
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?
推荐答案
length()== n
的字符串具有从0到n的有效索引1;
A String of length() == n
has valid indices from 0 to n-1;
更改
for(int i=0; i<=n; i++)
到
for(int i=0; i<n; i++)
这篇关于字符串索引超出绑定异常,字符串索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文