Java - “String index out of range” exception

前端 未结 5 1976
再見小時候
再見小時候 2021-01-16 04:14

I wrote this little function just for practice, but an exception (\"String index out of range: 29\") is thrown and I don\'t know why...

(I know this isn\'t the best

5条回答
  •  甜味超标
    2021-01-16 05:04

    Looks like you are a C/C++ programmer coming to java ;)

    Once you have gone out of range with .charAt (), it doesn't reach null, it reaches a StringIndexOutOfBoundsException. So in this case, you will need a for loop that goes from 0 to y.length()-1.

提交回复
热议问题