-1

I am trying to convert upper case letters to lower case and I get the following error:

 animalException in thread "main" java.lang.StringIndexOutOfBoundsException: String    index out of range: 6
    at java.lang.String.charAt(Unknown Source)
    at newproject.chars.main(chars.java:11)

Here is my code:

public class chars {
    public static void main(String[] args) { 

          String a = "AnImAl";
          for(int b=0;b<=a.length();b++) {
          char c= a.charAt(b);
          if(c>=65 && c<=90)
          {
             c=(char)((c + 32));
          }
          System.out.print(c);
    }
    }
}

Could somebody explain to me why I get this error? Thanks in advance

rgettman
  • 176,041
  • 30
  • 275
  • 357
alordlord
  • 359
  • 5
  • 15

1 Answers1

0

An index out of bounds error from a for loop is a quick indicator that the for loop iterates through too many nodes! Change your for loop to look like int b=0;b<a.length();b++