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

String index out of range  RSS feed

 
Mike Conors
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I am experimenting with some ai software, so I'm not that familiar with the source, which I don't think has been updated in a while. While running, a part that is supposed to create a graph throws this error:

By looking at the first place where the error shows up after the java.lang, I traced it to this snippet in "com.xapagy.ui.TextUiHelper.wrap":


How could I fix this error?

Thanks!
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know which value you are passing as width and which String you are passing as in. But might be you are getting error because the value of "place" is greater than length of String "in"
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error says the index is -1, which means place = -1. Probably your variable "in" does not contain a space, tab, or dash.
 
Mike Conors
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I fix that then?
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you fix that? Well, it's up to you - what do you want to return if the input string does not contain any of those characters? For example,
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

Avoid \n (even though you see it in many books) because it is platform‑specific. Use
myString + System.lineSeparator()
instead. That method only works in Java8; in older versions you would have to use something like
myString + System.getProperty("line.separator")
… or keep that separator as a constant somewhere.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!