I passed the task using the split() method, but the first solution I had also delivered the same output, but failed the test (code below):
public static void main(String[] args) {
System.out.println(getPartOfString("Amigo and Diego are best friends!"));
}
public static String getPartOfString(String string) throws StringTooShortException {
try {
int start = string.indexOf(" ") + 1;
int secondSpace = string.indexOf(" ", start + 1);
int thirdSpace = string.indexOf(" ", secondSpace + 1);
int fourthSpace = string.indexOf(" ", thirdSpace + 1);
int fifthSpace = string.indexOf(" ", fourthSpace + 1);
return string.substring(start, fifthSpace);
}catch (RuntimeException re){
throw new StringTooShortException();
}
}
public static class StringTooShortException extends RuntimeException {
}