This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error Code: StringIndexOutOfBoundsException: String index out of range: -1  RSS feed

 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Unclear what is causing the error (at line 7) ?



Bob M
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.substring(int beginIndex, int endIndex)
javadoc wrote:Throws:
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
 
Campbell Ritchie
Marshal
Posts: 55735
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
substring(4, 3)???

That is the problem.
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

have corrected the code to substring (4, 7)

Bob M
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're like me, you sometimes forget that the second parameter for substring is the ending index and not a length! This habit comes from my BASIC days.
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Knute

Yes - BASIC - those were the good ol' days

Regards from New Zealand

Bob M
 
Campbell Ritchie
Marshal
Posts: 55735
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You subtract the first number from the second number and you get the length of the substring.
 
Liutauras Vilda
Marshal
Posts: 4647
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course CR means when there is something to subtract.
You could have:
Which would give you an output of "ubtraction" (everything after 4th character (including fourth)).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!