Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 11

前端 未结 4 1595
别那么骄傲
别那么骄傲 2020-12-16 06:54

EVerytime I write any code similar to this one, I get this type of error. It\'s building a file but not letting it run, it just throws exception. I\'m not familiar with exce

4条回答
  •  无人及你
    2020-12-16 07:27

    for (int i=0; i<=name.length();i++){
    

    String indexes are starting from 0 .

    Example :

    String str = "abc";
    int len = str.length(); //will return 3
    

    str.charAt(3); will throws StringIndexOutOfBoundsException charAt starting position is 0 . So the limit is length-1 .

    You have to change your for loop to for (int i=0; i

提交回复
热议问题