not sure what is the issue . very eager to know the issue since output is as per the expectation i believe. there must be something minor i guess. pls. help
package com.codegym.task.task22.task2202;
public class Solution {
public static void main(String[] args) {
System.out.println(getPartOfString("Amigo and Diego are best friends"));
}
public static String getPartOfString(String string) throws StringTooShortException {
if (string == null) {
throw new StringTooShortException();
}
int spaceCounter = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == ' ') {
spaceCounter++;
}
}
if (spaceCounter < 4) {
throw new StringTooShortException();
}
int firstIndexOfSpace = string.indexOf(" ") + 1;
int secondSpaceCounter = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == ' ') {
secondSpaceCounter++;
}
if (secondSpaceCounter == 5) {
string = string.substring(firstIndexOfSpace, i);
break;
}
}
return string;
}
public static class StringTooShortException extends RuntimeException {
}
}