Home > Software design >  Java String.charAt() throws java.lang.StringIndexOutOfBoundsException
Java String.charAt() throws java.lang.StringIndexOutOfBoundsException

Time:11-23

The following code should give me a String concatenated from all characters after a space in a sentence:

public class Main {
    public static void main (String[] args) {
        System.out.println(generateFromSentence("Hello World!"));

    }
    public static String generateFromSentence(String sentence) {
        String output = String.valueOf(sentence.charAt(0));
        for (int i = 0; i <= sentence.length(); i  ) {
            if (sentence.charAt(i) == ' '){
                String following = String.valueOf(sentence.charAt(i 1));
                output = output.concat(following);
            }
        }
        return output;
    }
}

Sadly it throws:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12
        at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
        at java.base/java.lang.String.charAt(String.java:693)
        at Main.generateFromSentence(Main.java:9)
        at Main.main(Main.java:3)

I don't understand why the index would be out of range, at String output = String.valueOf(sentence.charAt(0)); I specify Index 0 - should be the first character of my string, right? Thanks already.

CodePudding user response:

Your issue is not in line String.valueOf(sentence.charAt(0)); but in line if (sentence.charAt(i) == ' '). The reason is your for loop stoping condition. You must use i < sentence.length() instead of i <= sentence.length() as follows:

public class Main {
    public static void main (String[] args) {
        System.out.println(generateFromSentence("Hello World!"));

    }

    public static String generateFromSentence(String sentence) {
        String output = String.valueOf(sentence.charAt(0));
        for (int i = 0; i <= sentence.length(); i  ) {
            if (sentence.charAt(i) == ' '){
                String following = String.valueOf(sentence.charAt(i 1));
                output = output.concat(following);
            }
        }
        return output;
    }
}

Keep in mind that indexes start at 0, so you can't iterate over a List or Array starting at 0 up to and including the List or Array size.

Additionally, keep in mind that for the same reason your (and my) code above will fail if the sentence is "Hello World! " (mind the at the end of the sentence). The reason is the line String following = String.valueOf(sentence.charAt(i 1));. To get around this issue you need to add a condition to your if statement:

public class Main {
    public static void main (String[] args) {
        System.out.println(generateFromSentence("Hello World! "));

    }

    public static String generateFromSentence(String sentence) {
        String output = String.valueOf(sentence.charAt(0));
        for (int i = 0; i < sentence.length(); i  ) {
            if (sentence.charAt(i) == ' ' && ((i   1) < sentence.length())){
                String following = String.valueOf(sentence.charAt(i 1));
                output = output.concat(following);
            }
        }
        return output;
    }
}
  • Related