字符串索引超出绑定异常,字符串索引超出范围

Arv*_*ani 2 java netbeans exception

所以,我正在编写一个简单的程序来输入字符串并计算总数.米 所以,这是我的代码

for(int i=0; i<=n; i++)
    {
        if((str.charAt(i)=='m'))
        {
        } else {
            count++;
        }
    }
    System.out.println("The total number of m is "+count);
Run Code Online (Sandbox Code Playgroud)

where n=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
Run Code Online (Sandbox Code Playgroud)

什么是这个错误以及如何删除它?

Era*_*ran 5

字符串的length() == n有效索引从0到n-1;

更改

for(int i=0; i<=n; i++)
Run Code Online (Sandbox Code Playgroud)

for(int i=0; i<n; i++)
Run Code Online (Sandbox Code Playgroud)