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
1
vote
4 answers

How to fix a String index out of range exception in Python

There is some issue with my python code. I am making a program that finds the occurrences of the letter A in a word and if that letter is found and the next letter is not the letter A the A is swapped with the next letter. As an example TAN being…
l.m
  • 131
  • 1
  • 10
1
vote
2 answers

StringIndexOutOfBoundsException when converting char[] read from BufferedReader in Android

I'm trying to read JSON-Strings through a socket connection in an Android app, but suddenly started getting StringIndexOutOfBoundsExceptions that I can't explain(blocked out some identifiers in the package…
1
vote
0 answers

string index out of bounds leads to build not being finished

In my android studio I'm not able to see my xml editor preview, everytime I try to see the window it displays this message: "waiting for build to finish". I suspect it to be the cause of a String index out of bounds exeption I'm having everytime I…
1
vote
0 answers

JavaFX Webview throws StringIndexOutOfBoundsException

On some websites the JavaFX Webview throws StringIndexOutOfBoundsExceptions. WebView browser = new WebView(); WebEngine webEngine = browser.getEngine(); setTop(browser); …
user6266369
  • 183
  • 1
  • 15
1
vote
3 answers

StringIndexOutOfBoundsException in a counting loop in Java

I have to write a code for class using Java in which the number of occurrences of the letter E is counted and printed out (both cases included). This is what I have. String sVerse = "As we enter our centennial year we are still young…
K3457
  • 11
  • 1
1
vote
0 answers

IndexError: string index out of range. Newbie at coding

I'm pretty beginner to Python so I get the error Traceback (most recent call last): File "C:/Users/spchee/Documents/cc.py", line 43, in c("helloz", 2) File "C:/Users/spchee/Documents/cc.py", line 35, in c add_to_string(n) File…
spchee
  • 61
  • 3
1
vote
2 answers

Java String index out of range : -1

lang.StringIndexOutOfBoundsException for the following code. The program checks for sentences without spaces to extract words which matches the dictionary of valid English words import java.util.ArrayList; import java.util.Arrays; import…
shailbenq
  • 1,440
  • 1
  • 14
  • 25
0
votes
2 answers

Java recursion Index out of range 1

I am currently trying to figure out why the cases that end with x results in an OutOfBoundsError like the 3 test cases below. The assignment is to count the number of 'x' in a string; if an 'x' is follow by another 'x' counted as double. Thank you,…
0
votes
2 answers

How to fix string out of index range when customizing looping "input()" in python?

I'm experimenting with the input() function. I'm trying to make the code to loop for user input and continue or break the loop based on the user input. while True: line = input("> ") if line[0] == "#" : continue if line == "done"…
0
votes
4 answers

Remove a Character From String in Java

I'm trying to concatenate a string with itself and remove all capital letters from the resultant string. Here is my code: public String removeCapitals(String A) { StringBuilder B = new StringBuilder(A+A); int n = B.length(); for(int…
0
votes
0 answers

wordly Guess game Error StringIndexOutofBound error

I am making a wordly type guess game using java swing ,trying to match two word (one from user input and second from txt file) and display it on gui label everything is working perfectly if i used the check_word as pre defined like String check_word…
user16870886
0
votes
0 answers

java right to left evaluator turning into a left to right evaluator, StringIndexOutOfBoundsException java

I have an assignment where I need to transform a right to left evaluator program into a left to right evaluator program(also make it so where it asks you to evaluate multiple expressions with a Y/N prompt). there were several changes I needed to…
0
votes
0 answers

Fatal error: String index is out of bounds Swift

I'm trying to get the substring of text typed in UITextView. Here's my code: let textString = state.textString let replacementTextRange = state.replacementRange let substringStart = textString.text.index( …
userNew
  • 11
  • 3
0
votes
1 answer

Java- Character array accepting entire string even if declared of lesser size

I have been trying to understand string handling techniques in Java, while I came across that my code is accepting the entire string that is initialized to the string object to a character array of lesser size than the number of characters in that…
0
votes
0 answers

my logic on this mastermind python works on paper, but when i use it in python it gives me two different errors, one logic and the other string index

for a school project i am creating the game master mind in python, if you dont know how master mind works the basic jist of it is to crack the code through known correct positions of the same colors and incorrect positions of the same…