Подскажите пожалуйста, в чем может быть проблема? Решал двумя способами, ответ верный при разных тестах.
package com.javarush.task.task22.task2202;
/*
Найти подстроку
*/
public class Solution {
public static void main(String[] args) {
System.out.println(getPartOfString("JavaRush - лучший сервис обучения Java."));
// System.out.println(getPartOfString("Амиго и Диего лучшие друзья!"));
}
public static String getPartOfString(String string) throws TooShortStringException {
if (string == null || string.isEmpty()) throw new TooShortStringException();
int count = 0;
int index = string.indexOf(" ");
//int index2 = string.in
int index2 = 0;
char[] a = string.toCharArray();
for(int i=0; i < a.length; i++)
{
if(String.valueOf(a[i]).equals(" "))
{
count++;
}
if(count == 4)
{
index2 = i;
}
}
if (count < 4) {
throw new TooShortStringException();
}
string = string.substring(index, index2+1);
return string;
/* String[] words = string.split(" ");
String s = " ";
if (words.length < 5) throw new TooShortStringException();
for(int i=0; i < words.length; i++)
{
if(i > 0 && i < 5)
{
s = s + words[i] + " ";
}
}
string = s;
return string;*/
}
public static class TooShortStringException extends RuntimeException {
}
}