Please help me, i tried it with string.length()-1 and string.length() and I doesn`t work both ways.
Greetings Fabian
package com.codegym.task.task22.task2202;
/*
Find a substring
*/
public class Solution {
public static void main(String[] args) {
try {
System.out.println(getPartOfString("CodeGym is the best place to learn Java."));
}
catch( Exception e) {
throw new StringTooShortException();
}
}
public static String getPartOfString(String string) throws StringTooShortException {
if(string==null) {
throw new StringTooShortException();
}
int spaceCount = 0;
for (char c : string.toCharArray()) {
if (c == ' ') {
spaceCount++;
}
}
if(spaceCount<4) {
throw new StringTooShortException();
}
int firstCharacterIndex=string.indexOf(" ")+1;
int lastCharacterIndex=string.lastIndexOf(" ")+1;
return string.substring(firstCharacterIndex,string.length());
}
public static class StringTooShortException extends RuntimeException {
}
}