0

I have this simple Java code which gives the following error: java.lang.StringIndexOutOfBoundsException: String index out of range: -4

the code is:

String date = "14000101";
String repayDate = date.substring(0, 4)+"-"+date.substring(5, 2)+"-"+date.substring(6, 2);

the length of the string is 8 and yet it gives an error starting form the second part.

Any ideas?

Thanks!

user2597012
  • 581
  • 4
  • 9
  • 28
  • 1
    read javadoc for java.lang.String.substring(int, int) – JIV Jul 04 '17 at 06:28
  • According the doc; Throws `IndexOutOfBoundsException` - if beginIndex or endIndex are negative, if endIndex is greater than length(), or if beginIndex is greater than startIndex – Mustafa Çil Jul 04 '17 at 06:29
  • Second argument of substring method is not the legnth but the end position of the substring. So, you want something like:date.substring(0, 4)+"-"+date.substring(5, 7)+"-"+date.substring(6, 8); – jolumg Jul 04 '17 at 06:34
  • The really interesting question here: why do we have such great existing question for ArrayIndexOutOfBounds, and NPE, ... but not for this stuff? – GhostCat Jul 04 '17 at 06:37
  • 1
    Possible duplicate of [getting java.lang.StringIndexOutOfBoundsException error](https://stackoverflow.com/questions/13517711/getting-java-lang-stringindexoutofboundsexception-error) – seenukarthi Jul 04 '17 at 07:07

5 Answers5

3

date.substring(5, 2) should be date.substring(5, 7) and date.substring(6, 2) should be date.substring(6, 8).

The second argument is the index of the character following the last character of the desired sub-string.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

You are givin negative values:

[Api][1] [1]: http://www.w3api.com/wiki/Java:String.substring()

public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)

If you put beginIndex lower that endIndex, crash.

You should do something like this:

String repayDate = date.substring(0, 4)+"-"+date.substring(5, 7)+"-"+date.substring(6, 8);

Try it! Good luck.

Diego Martin
  • 113
  • 5
0
substring(int beginIndex, int endIndex) 

In your code, endIndex is lesser than beginIndex. Check this

beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.

IndexOutOfBoundsException - if the beginIndex is negative, or 
endIndex is larger than the length of this String object, or 
beginIndex is larger than endIndex.
Community
  • 1
  • 1
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
0

java is kind of different to python....

this is invalid as reflected in the doc

 substring(5, 2)

the second integer must be bigger than the first one

if (endIndex > value.length) {
      throw new StringIndexOutOfBoundsException(endIndex);
}

if you mean to get 2 chars after the first parameter then do something like

substring(5, 5 + 2);

or even better:

int beginIndex = 5;
String dateB = date.substring(beginIndex, beginIndex + 2);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

read javadoc for java.lang.String.substring(int, int)

String date = "14000101";
    String repayDate = date.substring(0, 4)+"-"+date.substring(4, 6)+"-"+date.substring(6, 8);
    System.out.println(repayDate);
rackdevelop247
  • 86
  • 1
  • 10