[Solved] JAVA java.lang.StringIndexOutOfBoundsException: String index out of range: -1 Example


The java.lang.StringIndexOutOfBoundsException is RuntimeException or Checked Exception which is thrown by the methods of String class during index operations which indicate accessing index is either negative or greater than size of string itself.

java.lang.StringIndexOutOfBoundException is subclass of the IndexOutOfBoundException class which is used to indicate that accessing index to either an  Array, Vector or String, is out of Range.

String Class Methods where StringIndexOutOfBoundsException happen:

String object index range is between 0 to String.length() -1 if object access beyond this range lower or greater will throw StringIndexOutOfBoundsException .

  • public char charAt(int index);
  • public String substring(beginIndex);
  • public String subString(beginIndex,endIndex);
  • public CharSequence subSequence(beginIndex,endIndex);
  • public static String valueOf(char[] data, int offset, int count)

Example : Below example is for print all possible SubString for String FacingIssuesOnIT will throw StringIndexOutOfBoundException. on run time because index conditions are not properly handled.

package example;

public class StringIndexOutOfBounExceptionExample {

	public static void main(String[] args) {
		String str = "FacingIssueOnIT";
		System.out.println("*********All Substring of FacingIssuesonIT");
		for (int i = 0; i <= str.length(); i++) {
			for (int j = i + 1; j <= str.length(); j++) {
				System.out.println(str.substring(i, j - i));
			}
		}
	}
}

Output :

*********All Substring of FacingIssuesonIT
F
Fa
Fac
Faci
Facin
Facing
FacingI
FacingIs
FacingIss
FacingIssu
FacingIssue
FacingIssueO
FacingIssueOn
FacingIssueOnI

a
ac
aci
acin
acing
acingI
acingIs
acingIss
acingIssu
acingIssue
acingIssueO
acingIssueOn
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.lang.String.substring(Unknown Source)
	at example.StringIndexOutOfBounExceptionExample.main(StringIndexOutOfBounExceptionExample.java:10)

Issues with above Example :
Actually, your right edge of your substring function may be lower than the left one. For example, when i=(size-1) and j=size, you are going to compute substring(size-1, 1). This is the cause of you error.

Modified Example:

When you compare with above example loop condition are change SubString above range selection is modified.

package example;

public class StringIndexOutOfBounExceptionExample {

	public static void main(String[] args) {
		String str = "FacingIssueOnIT";
		System.out.println("*********All Substring of FacingIssuesOnIT");
		for (int i = 0; i <=str.length()-1; i++) {
			for (int j = i+1 ; j <= str.length(); j++) {
				//System.out.println("i="+i+",j="+ j);
				System.out.println(str.substring(i, j));
			}
		}
	}
}

Output:

*********All Substring of FacingIssuesOnIT
F
Fa
Fac
Faci
Facin
Facing
FacingI
FacingIs
FacingIss
FacingIssu
FacingIssue
FacingIssueO
FacingIssueOn
FacingIssueOnI
FacingIssueOnIT
a
ac
aci
acin
acing
acingI
acingIs
acingIss
acingIssu
acingIssue
acingIssueO
acingIssueOn
acingIssueOnI
acingIssueOnIT
c
ci
cin
cing
cingI
cingIs
cingIss
cingIssu
cingIssue
cingIssueO
cingIssueOn
cingIssueOnI
cingIssueOnIT
i
in
ing
ingI
ingIs
ingIss
ingIssu
ingIssue
ingIssueO
ingIssueOn
ingIssueOnI
ingIssueOnIT
n
ng
ngI
ngIs
ngIss
ngIssu
ngIssue
ngIssueO
ngIssueOn
ngIssueOnI
ngIssueOnIT
g
gI
gIs
gIss
gIssu
gIssue
gIssueO
gIssueOn
gIssueOnI
gIssueOnIT
I
Is
Iss
Issu
Issue
IssueO
IssueOn
IssueOnI
IssueOnIT
s
ss
ssu
ssue
ssueO
ssueOn
ssueOnI
ssueOnIT
s
su
sue
sueO
sueOn
sueOnI
sueOnIT
u
ue
ueO
ueOn
ueOnI
ueOnIT
e
eO
eOn
eOnI
eOnIT
O
On
OnI
OnIT
n
nI
nIT
I
IT
T

How to avoid StringIndexOutOfBoundException?

  • Debug program throughly after printing index values.
  • During operation check index value range between 0 to String.length()-1.
Advertisements

About Saurabh Gupta

My Name is Saurabh Gupta, I have approx. 10 Year of experience in Information Technology World manly in Java/J2EE. During this time I have worked with multiple organization with different client, so many technology, frameworks etc.
This entry was posted in Example, Exception, Java, java.lang.StringIndexOutOfBoundException and tagged , , , , . Bookmark the permalink.