We help IT Professionals succeed at work.
Get Started

wordEnds java challenge

454 Views
Last Modified: 2016-04-01
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;
  
}

Open in new window

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
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 47 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE