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

I can't find the error in main class StringIndexOutOfBounds

this is the first class this is the template class public class Email{ private String title; private String to; private double size; private boolean sent; static int numEmails; public Email(String Title, String To, double Size){ …
0
votes
1 answer

How to solve those exceptions in java swing

I am trying to make a simple UI in java swing that will ask user to input text. Here is my program: package practice; import java.awt.*; import javax.swing.*; public class gui_demo { JFrame frame; JLabel lblEnterText; static…
Titir
  • 1
  • 3
-1
votes
1 answer

StringIndexOutOfBoundsException when trying to count 'a's in the entered word in Java

public class methods { public static int howMany(String word) { char character = 'a'; int a = 0; for (int i=0;i<=word.length();i++) { if (word.charAt(i)==character) { a++; } …
Emir Can
  • 3
  • 1
-1
votes
1 answer

Comparing the first and last character of a string in python

first_and_last function returns True if the first letter of the string is the same as the last letter of the string, False if they’re different, by accessing characters using message[0] or message[-1]. While checking the condition for an empty…
Annie
  • 21
  • 2
  • 10
-1
votes
2 answers

java.lang.StringIndexOutOfBoundsException: String index out of range: 45

I'm trying to make a program that takes in a string like: KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG And returns something like this: $K13BCC$D15$K5MNUUU$G5 Another example is XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAKK Returns:…
Fire assassin
  • 81
  • 1
  • 9
-1
votes
3 answers

How is my python code going out of bound?

I've been trying to encode a string (ex: aabbbacc) to something like a2b3a1c2 this is the code i've tried: string_value = "aabbbacc" temp_string = "" for i in range(0, len(string_value)): if i != len(string_value) or i > len(string_value): …
-1
votes
1 answer

Java StringIndexOutOfBoundsException encountered when reading substring

So I have this java class which reads a file with multiple lines and whenever it reaches this part F C Y It's supposed to read the values of that (return F, C, Y) I've tried ClassString = temp.substring(0, 20).trim(); but it always…
Gene Yuson
  • 15
  • 5
-1
votes
2 answers

How to check if a certain character is present in the String?

I have wrote a below code to find a keyword co_e in the below string, where _ represents any other character. It works good if I change the String to "aaacodebbb" or "codexxcode" but if I change it to "xxcozeyycop" it throws…
gs650x
  • 383
  • 2
  • 22
-1
votes
2 answers

Model to avoid out of bounds exception in Java

I am reading data from a file, one line at a time. The number of elements in the line can vary. I am using Java 8. public boolean display (Integer code, String line) { String[] strArray = line.split(Constant.sep); String name =…
userDSSR
  • 627
  • 1
  • 8
  • 17
-1
votes
1 answer

Caesar Cipher code error: String index out of bounds

I'm doing a Caesar Cipher assignment for my grade 11 computer science class. I looked over my code many times, changing it, but nothing I do works and I don't understand why. The error it's giving me is…
Ren
  • 1
  • 2
-1
votes
2 answers

StringIndexOutOfBoundsException error while parsing JSON

I have been using this helper class to parse JSON data for a while now but upon doing updates the app crashes the 2nd time it is used at any point while the app is running. I can't seem to figure out why this is happening. Update 2 The issue is with…
-2
votes
1 answer

Mutliply characters in Java

public class String_multiply { public static void main(String[] args) { Scanner s = new Scanner(System.in); String str = s.next(); int len = str.length(); int i=0; String c_s = new String(); …
-2
votes
1 answer

Would appreciate some assistance finding StringIndexOutOfBoundsException

This program is the String of 3 numbers then 7 letters to be transformed into its equivalent number using methods The user input is a string which is changed from letters to digits and back into a string of numbers separated by dashes. So this is…
Lee
  • 21
  • 3
-2
votes
2 answers

Java String Error Out Of Bounds

I am exercising for my course in java and the task is to write a program which has the input of a list separeted with spaces. And the key is to turn the list around, i.e. put the first place on the last second on the one before last and truncate the…
-2
votes
1 answer

I cant get out the Element of an JList?

I have an JList and I want to get the information of the Element. This is my toString output and I want to get the Element of Kundennummer. Privatkunde [Vorname= Max| Nachname= Mustermann| Telefonnummer= 017632447658| E-Mail= musterman@max.de|…