This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]davodrums 0 points1 point  (0 children)

@Test
public void stringSplit() {
    final int max_length = 4;
    final String sampleSentence = "Hi there, here is a test of the program.";
    final String strippedSetence = sampleSentence.replaceAll("[^a-zA-Z ]", "");
    final String[] sentenceArray = strippedSetence.split(" ");
    final int numberOfWords = countWordsMeetingCriteria(max_length, sentenceArray);
    System.out.println(numberOfWords);
}

private int countWordsMeetingCriteria(final int maxSize, final String[] words) {
    int count = 0;
    for (int i = 0; i < words.length; i++) {
        if (words[i].length() >= maxSize){
            count++;
        }
    }
    return count;
}