If someone wants to explain to me how my version doesn't get the exact same version CodeGym's solution generates, I would love to hear it! Thank you!
package com.codegym.task.task22.task2202;
/*
Find a substring
*/
public class Solution {
public static void main(String[] args) throws StringTooShortException {
System.out.println(getPartOfString("CodeGym is the best place to learn Java."));
//System.out.println(getPartOfString("Amigo and Diego are best friends!"));
//System.out.println(getPartOfString(""));
}
public static String getPartOfString(String string) throws StringTooShortException {
try {
int indexOfFirstSpace = string.indexOf(" ");
int spaceCount = string.indexOf(" ");
for(int i=0; i<4; i++) {
spaceCount = string.indexOf(" ", spaceCount+1);
}
return string.substring(indexOfFirstSpace+1, spaceCount);
} catch (Exception e) {
throw new StringTooShortException();
}
}
public static class StringTooShortException extends RuntimeException {
}
}