Questions tagged [stringindexoutofbounds]

A StringIndexOutOfBounds exception is an exception thrown by String methods to indicate that the index of its character is out of bounds: i.e. either negative, or greater (sometimes equal to) the length of the string. Use this tag when you are indexing Strings and you unexpectedly face a StringIndexOutOfBounds.

A StringIndexOutOfBounds is a runtime exception in Java when you use a String method involving indexing one of its characters, but the index is out of bounds: negative or greater (sometimes equal to) the length of the String. Note that the indices of the characters of a String starts at zero, much like the indices of arrays.

If you index a String out of bounds, Java will throw a StringIndexOutOfBoundsException. It extends IndexOutOfBoundsException which may happen when indexing an array out of bounds.

Here is an example case of a StringIndexOutOfBoundsException:

public static void main(String[] args) {
    String s = "ABC"; // s.length() is 3.
    System.out.println(s.charAt(3)); // Indexes run from 0 to 2.
}

When running this method, Java will throw an exception. Here is an example stack trace:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.charAt(Unknown Source)
    at m2APR.StringIndexOutOfBounds.main(StringIndexOutOfBounds.java:6)

The Oracle documentation can be found here: https://docs.oracle.com/javase/7/docs/api/java/lang/StringIndexOutOfBoundsException.html

66 questions
4
votes
2 answers

Understanding java.lang.StringIndexOutOfBoundsException: length=X; regionStart=Y; regionLength=Z

How to interpret a java.lang.StringIndexOutOfBoundsException: length=30278; regionStart=6877; regionLength=-12 or better how to reproduce such an exception where length is positive, regionStart is positive and less than length and regionLength…
ka3ak
  • 2,435
  • 2
  • 30
  • 57
3
votes
2 answers

Why does the following exception showing -4 index specifically instead of -1?

I am trying to get a substring out of a string. I have written logic to provide a start index and an end index but getting below exception. The code is: final int startIndex = header.indexOf(startValue + ":") + (startValue +…
Abhinandan
  • 87
  • 1
  • 12
3
votes
4 answers

Kotlin: Run length encoding

The program works, however, I still get a logical error: the final letter doesn't run through. For example, when I enter aaaabbbbccccdddd the output I get is a4b4c4 but there is no d4. fun main () { val strUser = readLine()!!.toLowerCase() …
3
votes
1 answer

subscript out of bounds error in R programming

Getting following error while using prophet library: Error in [<-(*tmp*, m$history$t >= m$changepoints.t[i], i, value = 1) : subscript out of bounds Code : m <- prophet(data) this data I've loaded from csv file. My dataset looks like this : …
Nikita Gupta
  • 495
  • 9
  • 24
2
votes
1 answer

"Out of bounds nanosecond timestamp"? How do you avoid this error?

I have an array, recognised as a 'numpy.ndarray object' which prints the following output when running the following code: with sRW.SavReaderNp('C:/Users/Sam/Downloads/Data.sav') as reader: record =…
user11357465
2
votes
2 answers

StringIndexOutOfBoundsException unclear

My goal is to return a string that is composed of each letter as long as the letter is later on in the alphabet than its previous one, assuming the word is lowercase. Problem is, I can't even test this out because I am getting an error (in the…
2
votes
0 answers

Error message(java.lang.StringIndexOutOfBoundsException) when trying to connect oracle database from virtualbox to Java(Eclipse) using JDBC

I am getting this error message "java.lang.StringIndexOutOfBoundsException: String index out of range: 25978" when I am trying to run following codes: import java.sql.*; public class test_conn { public static void main(String[] args) { try { …
Connor
  • 21
  • 3
1
vote
1 answer

How to make permutation program using StringBuilder in java?

I am willing to create a program that outputs all the possible permutations of a string. For example: Input abc Output abc acb bac bca cab cba I am able to get this output when I use String in java, but I am curious do the same using StringBuilder…
1
vote
1 answer

Couting Word Frequency

public static void CountWordFrequency(ArrayList UserString) { //creating an array list to store every word //each element in the UserString is one line ArrayList words_storage = new ArrayList(); …
1
vote
2 answers

Problem in taking a string as an Input in JAVA

I am trying to create a program for taking ad IP address as input and give Class, netID, and hostID as output. I am having a problem in taking IP address as String input in the program. This is the snap of the error i am getting Error: Exception in…
1
vote
1 answer

i tried to add viewPager to my project but there is problem

The issue is caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 at com.example.proje.DetayActivity.onCreate(DetayActivity.java:78) This page is where i add Viewpager to be shown on the activity. I think there is problem…
1
vote
1 answer

Java Out of Bounds exception on String input parameter for Constructor

I'm new to Java and I keep trying to debug my code to no avail. I'm trying to use arrayList to build an array for a newEmployee and append it an arraylist of employees ( ArrayList> ). The dateParse, nameParse and addressParse are separate methods…
1
vote
3 answers

Checking for consecutively repeated characters in java

I am quite new to java. I am wondering if it is possible to check for a certain number of consecutively repeated characters (the 'certain number' being determined by the user) in a string or an index in a string array. So far I have tried int…
Malted_Wheaties
  • 132
  • 1
  • 10
1
vote
3 answers

Substring index out of bounds in java

I am writing a simple code that displays only the name of the processes which are of "console" type using tasklist in java. I am unable to do so because of the string index out of bounds error in this code. I used index 36 to 43 because in these I…
user12459907
1
vote
2 answers

Why am I getting this error, eventhough I am getting result?

Getting exception in the code below: import java.io.*; public static void main(String[] args) throws IOException{ FileReader objRead = new FileReader("/home/acer/Desktop/sulabh"); BufferedReader objB = new BufferedReader(objRead); …
1
2 3 4 5