Both issues are correct but the last point is not met ... why?
package com.codegym.task.task22.task2202;
import java.util.*;
/*
Find a substring
*/
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) throws StringTooShortException {
if (string == null)
throw new StringTooShortException();
int count = 0;
for (int i = 0; i < string.length(); i++) {
char anzahl = string.charAt(i);
if (anzahl == ' ') {
count++;
}
}
if(count<5) throw new StringTooShortException();
int count1 = 0;
int index1 = 0;
int index2 = 0;
ArrayList<Character> array = new ArrayList<>();
for (int i = 0; i < string.length(); i++) {
char d = string.charAt(i);
array.add(d);
if (array.get(i) == ' ') {
count1++;
if (count1 == 1) {
index1 = i;
} else if (count1 == 5) {
index2 = i;
}
}
}
return string.substring(index1, index2);
}
public static class StringTooShortException extends RuntimeException {
}
}