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
0
votes
1 answer

Run Time Error_String index out of bound Exception_Printing string odd and even indexes

Code : import java.io.; import java.util.; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] sa = new String[n]; for(int i=0;i
Siva
  • 17
  • 7
0
votes
1 answer

Simple assignment of a variable to string function

I am brand new in programming. I keep getting the error what(): basic_string::at: __n (which is 14) >= this->size() (which is 14) void longword(string word) { int length = word.length(); if (length > 10) { cout << word[0] <<…
0
votes
4 answers

Python: String index out or range in while loop while i try to covert a string into a list

I am trying to convert a string consisting of numbers separated by a hyphen ('-') into a list that contains only the numbers. For example:- if a string str="10-2-517-92" then the output should be: n=[10,2,517,92]. I have written this code so…
0
votes
0 answers

No Out_Of_Bounds error even after trying to access illegal element in string in c++

In the below code, I have initialised an empty string 'a' and now I am arbitrarily trying to print any value out of it. Please help me understand why it does not show any error and prints garbage value instead! #include #include…
0
votes
1 answer

Return rows of df of particular month and year python pandas OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:00

I am trying to create a function that will return rows pertaining only to particular month and year: df order_date Type 2015-01-01 A 2017-09-01 A 2016-12-19 C 2019-11-23 D 2018-10-29 B 2017-12-31 B 2015-11-30 …
noob
  • 3,601
  • 6
  • 27
  • 73
0
votes
1 answer

StringIndexOutOfBoundsException in PrefixtoPostFix Stack (Java)

There's two classes in this project. The Main method is the Test2 and the child class is the Stack. The error part here is this part: Stack s = new Stack(20); And whenever I run it, it outputs like this: Exception in thread "main"…
Star-Lord
  • 73
  • 1
  • 8
0
votes
1 answer

Map-reduce using java - java.lang.StringIndexOutOfBoundsException: String index out of range: 0

I am trying to write a Spark application which outputs the number of words that start with each letter. I am getting a String index out of range error. Any suggestions, or am I not approaching this map-reduce problem in the right way? public class…
walter_dane
  • 115
  • 4
  • 8
0
votes
1 answer

Index 4 out of bounds for length 3. For Union Array

//Whenever I run the code, it keeps giving me the same error. I don't understand why. The union array function's loop also isn't looping completely, likely a problem with the string index out of bound. //I've tried to change the original function…
Michael
  • 1
  • 1
0
votes
1 answer

Get Data From Excel - Index exceeds bounds

I am trying to get data from an Excel file. I know that indexing in excel starts from 1, but still when reading a simple matrix, I am getting an Index exceeds bounds error. Here is a simple method I'm using: public static string[,]…
Luki
  • 39
  • 5
0
votes
0 answers

Java method parsing text returning StringIndexOutOfBoundsException only for positive numbers

I have a method that parses line of text, such as 24/11/2018, Drive, -2.00, and returns the number before the last comma, this case being -2. Here is the method: public static int value (String line){ //-5 becasue of characters after dot, t1…
Puki
  • 157
  • 1
  • 2
  • 8
0
votes
2 answers

Java How to avoid String Index out of Bounds

I have the following task to do: Return true if the string "cat" and "dog" appear the same number of times in the given string. catDog("catdog") → true catDog("catcat") → false catDog("1cat1cadodog") → true my code: public boolean…
user8953265
  • 21
  • 2
  • 11
0
votes
0 answers

Move files to folders with same name in Java

I have files and folders with same name(without characters of extensions). I try to move each files to the right folders with following code. private static void moveFile() { File directory = new File(GlobalVariables.DOWNLOAD_FOLDER_ROOT); …
plaidshirt
  • 5,189
  • 19
  • 91
  • 181
0
votes
0 answers

string index out of bounds from Jsoup response

My android app gets StringIndexOutOfBound exceptions in some devices and also works perfectly otherwise in some devices here is my code try { Document document = Jsoup.connect(PostLink).header("Accept-Encoding", "gzip,…
0
votes
3 answers

How to see if first character of a string is a certain char, and if it is, perform an action (not working right)

import java.util.Random; import java.util.Scanner; public class MineSweeper { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); boolean playAgain = true; System.out.println("Welcome to Mine…
punchkey
  • 81
  • 1
  • 11
0
votes
1 answer

String substring(i,j) throw IndexOutOfBounds Exception in method() but not in main()

public class Longest { public static void main(String[] args) { String tc = "babad"; StringBuffer sb = new StringBuffer(tc.substring(0,tc.length())); System.out.println(sb.toString().equals(sb.reverse().toString()));…