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
-2
votes
3 answers

StringIndexOutOfBoundsException when using Character.getNumericValue

This is the accept function for my tic tac toe program, so s is only going to store data in String format and in between 0,0 or 2,2. I am now using the getNumericValue function to store the numbers in p and q respectively, but during runtime, I get…
-3
votes
1 answer

getting StringIndexOutOfBound Exception

I am writing a program to accept user name and password using following conditions- username must be min 8 characters. Password must have min 10 characters, 1 lowerCase, 1 upperCase, 1 digit should present in the password. I wrote a method…
-3
votes
2 answers

Can't find cause of out of bounds error

I'm getting this error: java.lang.StringIndexOutOfBoundsException: String index out of range: 7 and I don't know what's causing the out of bounds exception. Here's my code: public class LuckySeven { public int luckySevens(String input, int…
-3
votes
1 answer

StringIndexOutOfBoundsException JavaFx WebEngine

The following code generates a StringIndexOutofBoundsException and I'm not sure why: public class Browser extends Application { @Override public void start(Stage primaryStage) { WebPage currentPage = new WebPage(); WebView webView =…
hopoke
  • 11
  • 1
-4
votes
1 answer

Index or String out of bounds exception error

My program is supposed to find bad words in a given input, but sometimes I get an out of bounds error. What is the cause of this error? /* Andrew Woan - Alien Message Board Project - Period 1 */ import java.lang.Math; import…
-4
votes
2 answers

How do I get rid of StringIndexOutOfBoundsException in my code?

This is my first question on stackoverflow. I wanted to test myself and make a mini-programming language. But ever since I've decided to expand my code, I keep getting StringIndexOutOfBoundsExceptions. I've done some research here on stackoverflow…
1 2 3 4
5