String index out of bounds? (Java, substring loop)

前端 未结 5 645
感情败类
感情败类 2021-01-20 17:26

This program I\'m making for a COSC course isn\'t compiling right, I keep getting the error:

Exception in thread \"main\" java.lang.StringIndexOutOfBoundsException:

5条回答
  •  遇见更好的自我
    2021-01-20 18:06

    You may need to take out the = in the line

    while (count <= input.length() ) {
    

    and make it

    while (count < input.length() ) {
    

    because it is causing the substring to read beyond the length of the string.

    =============== But I'll add a few extra bits of advice even though its not asked for:

    do not use == to compare strings, use

    letter.equals("a")
    

    instead. Or even better, try using

    char c = input.charAt(count);
    

    to get the current character then compare like this:

    c == 'a'
    

提交回复
热议问题