• Post Reply Bookmark Topic Watch Topic
  • New Topic

codingbat - trying to solve te task  RSS feed

 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello guys! here is the task i have :
A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread.

getSandwich("breadjambread") → "jam"
getSandwich("xxbreadjambreadyy") → "jam"
getSandwich("xxbreadyy") → ""

here is my code :


when code compiles, i get the melding :Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -5 (line number:16)" ( getSandwich("xxbreadyy") → "" " ).what i did wrong in a code, that this example didnt go via if (b == -1 || bsecond == -1 ){ return "";} statement? Thank you!
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
xxbreadyy contains one instance of bread so indexOf and lastIndex of will return the same value 2.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!