the task is really simple, the code works, but I cant'n understand what is the problem.
package com.codegym.task.task22.task2202;
import java.util.*;
/*
Find a substring
The getPartOfString method must return the substring that begins with the character after the first
space and ends with the end of the word that follows the 4th space.
Example:
"CodeGym is the best place to learn Java."
Result:
"is the best place"
Example:
"Amigo and Diego are best friends!"
Result:
"and Diego are best"
If the input data is invalid, throw a StringTooShortException (create an exception).
Requirements:
• The StringTooShortException class must be a child of the RuntimeException class.
• The getPartOfString method must have a String parameter.
• If invalid data is passed to the getPartOfString method, a StringTooShortException should be thrown.
• The getPartOfString method must return the substring that begins with the character after the first
space and ends with the word that follows the 4th space.
*/
public class Solution {
public static void main(String[] args) {
System.out.println(getPartOfString("CodeGym is the best place to learn Java."));
}
public static String getPartOfString(String string) {
int startIndex, endIndex, counter = 0;
if (string == null) throw new StringTooShortException();
startIndex = string.indexOf(" ");
if (startIndex < 0) throw new StringTooShortException();
endIndex = startIndex;
while(counter < 4){
endIndex = string.indexOf(" ", ++endIndex);
if (endIndex < 0) throw new StringTooShortException();
counter++;
}
//System.out.println(startIndex + " " + endIndex); //Debugging
return string.substring(startIndex, endIndex);
}
public static class StringTooShortException extends RuntimeException{
}
}