Java Split Util Without Worrying About IndexOutOfBoundException

Last Modified: 2023/11/18

Overview

Today I'll share a method for splitting strings that will make it safer for you to use the result of the split function and keep you away from IndexOutOfIndexException.

The Troubles of the Java split method

When using the Java split method to split strings, it is often necessary to check the length of the resulting array. This is due to two reasons:

  • The data itself may not be consistently formatted. If the data being processed contains a varying number of separators, the length of the resulting array will naturally differ.
  • The number of separators does not determine the length of the resulting array. For example, if we want to split the string "a,b,c,,," using a comma as the separator, the actual length of the resulting array would be 3, not 6.

In either case, regardless of the scenario, when we attempt to assign the split result to other variables, we must be cautious and carefully check if the indices are out of bounds. Otherwise, we risk encountering an IndexOutOfBoundsException.

Let's consider a scenario where we have a text file to process. Each line of the file represents user information, including their name, age, email, and contact information. It's important to note that the email and contact information are optional.

MusicLover,18
StarGazer,20,stargazer@gmail.com
TechMaster,20,techmaster@gmail.com,18536428596

We've decided to define a splitLine function that will be responsible for parsing the data lines and returning a parsed User object.

User splitLine(String line) {
    User user = new User();
    String[] splits = line.split(",");
    user.name = splits[0];
    user.age = splits[1];
    // index check
    if (splits.length > 2) {
        user.email = splits[2];
    }
    // index check
    if (splits.length > 3) {
        user.mobile = splits[3];
    }
    return user;
}

The User model is defined as follows:

public class User {
    public String name;
    public String age;
    public String email;
    public String mobile;
}

Since the length of the array after splitting can range from 2 to 4, we need to perform index validation when assigning the email and mobile fields. If accessing an array index out of bounds doesn't throw an error but instead returns null, would that solve the problem?

While this is just a hypothetical scenario, Java does not support accessing array elements out of bounds for safety reasons. However, for this specific problem, we can consider encapsulating a split method that returns a SplitResult object. This object would allow access to any index, but if an out-of-bounds index is accessed, it would return null instead of throwing an error.

Encapsulating the return result of the Java split method

public class SplitResult {
    private final String[] splits;

    public SplitResult(String[] splits) {
        this.splits = splits;
    }

    public String get(int index) {
        return index >= this.splits.length ? null : this.splits[index];
    }
}

Now let's implement the split util method:

public static SplitResult split(String str, String delimiter) {
    String[] splits = str.split(delimiter);
    return new SplitResult(splits);
}

With this split implementation, let's rewrite the splitLine method mentioned above:

User splitLine(String line) {
    User user = new User();
    SplitResult splits = split(line, ",");
    user.name = splits.get(0);
    user.age = splits.get(1);
    user.email = splits.get(2);
    user.mobile = splits.get(3);
    return user;
}

Compared to the previous implementation, the code is much more concise, and there is no longer a concern about array index out-of-bounds errors. Click here to view the complete code.

Feedback

Notice:Feedback requires logging into the system first.