Check if String[index] is empty before throwing ArrayIndexOutOfBounds exception

Check if String[index] is empty before throwing ArrayIndexOutOfBounds exception

This probably is a silly question but I am missing something and just can't figure out what... Looking for a sensible way of checking whether given index exists or not in a String[] array

This probably is a silly question but I am missing something and just can't figure out what... Looking for a sensible way of checking whether given index exists or not in a String[] array

String is a representation of a K,V pair but sometimes V may be empty so possible string examples are:

Foo1:Bla1

Foo2:Bla2

Foo3:

Foo4:Bla4

public void constructPair(String string) {
    String[] split = string.split(":");
    ...
    if(split[index] != null) { } // nope
    if(!split[index].isEmpty() || !split[index].isBlank() { } //nope
    if(split[index].length() > 1) { } // nope
    ...
}

Or should i just wrap the whole thing in try {} catch() {} ? block and deal with the exception accordingly?

Edit: For clarity 'index' is just pseudo not actual variable name

java arrays string

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java String Class Example | String Class in Java Tutorial

Java String is an inbuilt class that deals with all that is written in double quotation marks, or other words string is a collection of characters.

How to convert an Array to String in Java?

In this post, you'll learn the various methods to convert an Array to String in Java

Java Arrays Example | Arrays in Java Explained

Java Arrays store a value into the variable. Now if we required more values then have to declare more variables, i.e., one variable for one value.

Java String Programs For Beginners | Java String Examples | Java Training

Java String Programs will talk about top 10 important programs in java strings with examples.