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
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?
count++should be in theifblock, not theelseblock, since you are trying to count the number of m's.