troubleshooting Question

wordEnds java challenge

Avatar of gudii9
gudii9Flag for United States of America asked on
JavaJava EEProgrammingRegular ExpressionsProgramming Languages-Other
47 Comments1 Solution462 ViewsLast Modified:
Hi,
I am working on below challenge
http://codingbat.com/prob/p147538
I tried my code as below
public String wordEnds(String str, String word) {
int xyPos=str.indexOf("xy");
String s1=str.substring(xyPos,xyPos-1);
String s2=str.replaceAll(str,word);
String s3=s1+s2;
return s3;
  
}
I am getting below result
Expected      Run            
wordEnds("abcXY123XYijk", "XY") → "c13i"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XY123XY", "XY") → "13"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XY1XY", "XY") → "11"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XYXY", "XY") → "XY"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XY", "XY") → ""      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("Hi", "XY") → ""      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("", "XY") → ""      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1i1j", "1") → "cxziij"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1", "1") → "cxz"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz11", "1") → "cxz11"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1abc", "abc") → "11"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1abc", "b") → "acac"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1abc1abc", "abc") → "1111"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
other tests
X         
Your progress graph for this problem

how to  improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
 Please advise
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 47 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 47 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros