String index out of range

In Java, “String index out of range” exception is thrown in java substring() & charAt() method. Here, we see the exception “java.lang.StringIndexOutOfBoundsException: String index out of range: 0“. If a invalid value is passed in begin and end index value, “java.lang.StringIndexOutOfBoundsException: String index out of range” will be thrown. The StringIndexOutOfBoundsException is thrown if you try to substring or find a character in a java string.

A subset of the sequence of characters can be extracted from a string by using the Java substring() method. The substring index should be any value between 0 and the length of a string. If the index exceeds the limit, substring method returns “String index out of range: 0” exception.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
	at java.lang.String.charAt(String.java:658)
	at com.yawintutor.StringSubstring.main(StringSubstring.java:8)


Root Cause

Using the substring method, a subset of the character sequence can be extracted from a string. The substring index must be any value from 0 to the length of a string. If the index exceeds the limit, StringIndexOutOfBoundsException is thrown in String substring method

If the index value in the error message is zero or positive value, the exception “String index out of range” is thrown using string.charAt() method. The error message is shown as below.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
	at java.lang.String.charAt(String.java:658)
	at com.yawintutor.StringSubstring.main(StringSubstring.java:8)

If the index value in the error message is negative value or positive value, the exception “String index out of range” is thrown using string.substring() method. The error message is shown as below.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.lang.String.substring(String.java:1967)
	at com.yawintutor.StringSubstringCharAt.main(StringSubstringCharAt.java:8)


How to reproduce this exception using charAt()

The charAt() method is used to read a character in the java string. The parameter index is used to identify the location of the character in the string. If the index location is not available in the string, the error “java.lang.StringIndexOutOfBoundsException: String index out of range: 0” is thrown

package com.yawintutor;

public class StringSubstringCharAt {
	public static void main(String[] args) {
		String str = "";
		char ch;

		ch = str.charAt(0);

		System.out.println("Given  String   : " + str);
		System.out.println("Output Charcter : " + ch);
	}
}

Output

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
	at java.lang.String.charAt(String.java:658)
	at com.yawintutor.StringSubstring.main(StringSubstring.java:8)


How to reproduce this exception with Substring

If the value of the substring index is set above the limit of the string length, this exception “java.lang.StringIndexOutOfBoundsException: String index out of range: -1” will occur. The example below reproduces this exception.

package com.yawintutor;

public class StringSubstringCharAt {
	public static void main(String[] args) {
		String str = "Bangalore";
		String str2;

		str2 = str.substring(3, 1);

		System.out.println("Given  String : " + str);
		System.out.println("Output String : " + str2);
	}
}

Output

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
	at java.lang.String.substring(String.java:1967)
	at com.yawintutor.StringSubstringCharAt.main(StringSubstringCharAt.java:8)


Solution 1

The java string contain an empty value. If the string is null, then NullPointerException will be thrown. As the java program thrown with index 0, “java.lang.StringIndexOutOfBoundsException: String index out of range: 0”, the java string may be an empty string.

Check the string or character sequence value. Make sure the string contains value or add the empty string validation in the code.

package com.yawintutor;

public class StringSubstringCharAt {
	public static void main(String[] args) {
		String str = "";
		char ch='\0';

		if(str!=null && str.length()!=0) {
			ch = str.charAt(0);
		}

		System.out.println("Given  String   : " + str);
		System.out.println("Output Charcter : " + ch);
	}
}

Output

Given  String   : 
Output Charcter : 


Solution 2

The first parameter of the substring is begin index.

  • The value of the begin index should be zero or greater than zero.
  • The value of the begin index should not be more than the length of the string


Solution 3

The second parameter of the substring is end index. The end index is a optional value. If the end index is not passed, by default it is considered till end of the given string.

  • The value of the end index should be zero or greater than zero
  • The value of the end index should not be more than the length of the string
  • The value of the end index should be greater than or equals to the value of the start index.
package com.yawintutor;

public class StringSubstringCharAt {
	public static void main(String[] args) {
		String str = "Bangalore";
		String str2;

		str2 = str.substring(3, 8);

		System.out.println("Given  String : " + str);
		System.out.println("Output String : " + str2);
	}
}

Output

Given  String : Bangalore
Output String : galor



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *