public static String getPartOfString(String string) throws TooShortStringException {
if (string == null || string.isEmpty()) throw new TooShortStringException();
int count = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == ' ') {
count++;
}
}
String result = "";
if (count < 4) {
throw new TooShortStringException();
} else {
String[] mas = string.split(" ");
for (int i = 1; i < 5; i++) {
result += mas[i] + " ";
}
}
return result;
}package com.javarush.task.task22.task2202;
/*
Найти подстроку
*/
public class Solution {
public static void main(String[] args) throws RuntimeException {
System.out.println(getPartOfString("JavaRush - лучший сервис обучения Java."));
}
public static String getPartOfString(String string) throws TooShortStringException {
if (string == null || string.isEmpty()) throw new TooShortStringException();
int count = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == ' ') {
count++;
}
}
String result = "";
if (count < 4) {
throw new TooShortStringException();
} else {
String[] mas = string.split(" ");
for (int i = 1; i < 5; i++) {
result += mas[i] + " ";
}
}
return result;
}
public static class TooShortStringException extends RuntimeException {
}
}