Java - StringIndexOutOfBoundsException



StringIndexOutOfBoundsException is a runtime exception in Java that occurs when an invalid index is used to access a character in a string. This happens when the index is either negative or greater than or equal to the string’s length.

Following is the reason when JVM throws a StringIndexOutOfBoundsException in Java:

  • When an invalid index is used to access a character in a string, JVM throws a StringIndexOutOfBoundsException.

Constructors of StringIndexOutOfBoundsException

There are two constructors of StringIndexOutOfBoundsException class:

  • StringIndexOutOfBoundsException(): This constructor is used to create a StringIndexOutOfBoundsException object without any message.
  • StringIndexOutOfBoundsException(String message): This constructor is used to create a StringIndexOutOfBoundsException object with a message.

Methods of StringIndexOutOfBoundsException

There are some methods of StringIndexOutOfBoundsException class:

Method Description
getMessage() It is used to return the message of the exception.
toString() It is used to return the detail message string of the exception.
printStackTrace() It is used to print the stack trace of the exception.

Example of StringIndexOutOfBoundsException

In this example, let's try to access a character in a string using an invalid index, so JVM will throw a StringIndexOutOfBoundsException.

public class StringIndexOutOfBoundsExceptionExample {

   public static void main(String[] args) {
      String str = "tutorialspoint";
      char ch = str.charAt(30);  // This will throw StringIndexOutOfBoundsException
   }
}

Output

Following is the output of the above code:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 14
	at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
	at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
	at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
	at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
	at java.base/java.lang.String.checkIndex(String.java:4832)
	at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:46)
	at java.base/java.lang.String.charAt(String.java:1555)
	at StringIndexOutOfBoundsExceptionExample.main(StringIndexOutOfBoundsExceptionExample.java:5)

As you can see in the output, JVM throws a StringIndexOutOfBoundsException because we are trying to access a character in a string using an invalid index.

Handling StringIndexOutOfBoundsException

In this example, let's try to access a character in a string using an invalid index, so JVM will throw a StringIndexOutOfBoundsException. We are handling this exception using a try-catch block.

public class StringIndexOutOfBoundsExceptionExample {

   public static void main(String[] args) {
      String str = "tutorialspoint";
      
      try {
         char ch = str.charAt(30);  // This will throw StringIndexOutOfBoundsException
      } catch (StringIndexOutOfBoundsException e) {
         System.out.println("String index out of range");
      }
   }
}

Output

Following is the output of the above code:

String index out of range

How to avoid StringIndexOutOfBoundsException?

Following are the ways to avoid StringIndexOutOfBoundsException in Java:

  • Always use a try-catch block while accessing a character in a string using an index.
  • Check the length of the string before accessing a character using an index.

Check the index before accessing a character in a string

It is always a good practice to check the index before accessing a character in a string. This will help you avoid a StringIndexOutOfBoundsException.

public class StringIndexOutOfBoundsExceptionExample {

   public static void main(String[] args) {
      String str = "tutorialspoint";
      int index = 30;
      if (str.length() > index) {
         char ch = str.charAt(index);  // This will not throw StringIndexOutOfBoundsException
      } else {
         System.out.println("String length is less than the index, so can't access the character");
      }
   }
}

Output

Following is the output of the above code:

String length is less than the index, so can't access the character

As you can see in the output, we are checking the length of the string before accessing a character using an index. So, JVM does not throw a StringIndexOutOfBoundsException.

java_lang_exceptions.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy