user208829 user208829 - 4 months ago 13
Java Question

Receing StringIndexOutOfBoundsException error in code for outputting substring to the end of the sentence

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Untitled {

public static void main(String[] args) throws IOException {
String content = new String(Files.readAllBytes(Paths.get("YUGECORPUS.txt")));
content = content.replace("\n", " ").replace("\r", " ");
String search = "George Bush is";
System.out.print(content.substring(content.indexOf(search), content.substring(content.indexOf(search)).indexOf(".")));


The error I receive when compiling the code is the following:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -3073945
at java.lang.String.substring(
at Untitled.main(

How do I fix this error, and why is it occuring? The purpose of the following code in the snippet above:

content.substring(content.indexOf(search), content.substring(content.indexOf(search)).indexOf("."))

Is print the text from the String
from the beginning of the first occurrence of
to the first occurrence of a period


There is an error where you are trying to find the index of .:


will give you the index inside the substring, not the index inside content. To fix you should add the start index of search to it.

Also if content does not contains your search string and consecutively a . your code might also not work correctly and generate exceptions.

To make the code more error proof you could add checks if content contains what you expect like so (note the fix at endIndex += startIndex):

int startIndex = content.indexOf(search);
if(startIndex > -1) {
    int endIndex = content.substring(startIndex).indexOf(".");
    if(endIndex > -1) {
        endIndex += startIndex;
        String foundString = content.substring(startIndex, endIndex);

Explicitly making the checks instead of putting everything in a one line also will make the code much easier to debug and find errors.