C:\Users\notebook\Documents\NetBeansProjects\CS2\src\review\LongerWords.java |
1
2
3
4
5
6 package review;
7
8 import java.util.ArrayList;
9 import java.util.Scanner;
10
11
12
13 @author
14
15 public class LongerWords {
16
17
18 @param args
19
20 public static void main(String[] args) {
21 System.out.print("Sentence? ");
22 Scanner scanner = new Scanner(System.in);
23 ArrayList<String> words = new ArrayList<>();
24 String sentence = scanner.nextLine();
25 String[] splitedSentence = sentence.split(" ");
26 for (int a = 0; a < splitedSentence.length; a = a + 1) {
27 words.add(splitedSentence[a]);
28 }
29 int sumOfWordLength = 0;
30 for ( int x = 0; x < words.size(); x = x + 1) {
31 String word = words.get(x);
32 int wordLength = word.length();
33 sumOfWordLength = sumOfWordLength + wordLength;
34 }
35 double avgWordLength = sumOfWordLength/words.size();
36 System.out.println("The longer words ...");
37 for ( int x = 0; x < words.size(); x = x + 1) {
38 if (words.get(x).length() > avgWordLength) {
39 System.out.println(words.get(x));
40 }
41 }
42 }
43
44 }
45