/home/sjenks/NetBeansProjects/CS2/src/review/LongerWords.java
 1 /*
 2  * This prograsm is to prompt the user for a statement with not punctuation and 
 3  * then print the words that are longer than the average word length. 
 4  */
 5 package review;
 6 
 7 import java.util.ArrayList;
 8 import java.util.Scanner;
 9 
10 /**
11  *
12  * @author sjenks
13  */
14 public class LongerWords {
15 
16     /**
17      * @param args the command line arguments
18      */
19     
20     public static void main(String[] args) {
21         ArrayList<String> words = getWords();
22         double average = findAverage(words);
23         getLongWords(words,average);
24       
25         
26     }
27 
28     private static ArrayList <String> getWords(){
29         ArrayList<String> words = new ArrayList<>();
30         Scanner scanner = new Scanner(System.in);
31         System.out.print("Sentence? ");
32         String sentence = scanner.nextLine();
33         Scanner reader = new Scanner(sentence);
34      
35        // System.out.print(sentence);
36         //the scanner. next line works, but the while loop doesn't 
37         
38         while (reader.hasNext()) {
39             String word = reader.next();
40             words.add(word);
41         }
42         return words;
43     }
44 
45 
46     private static double findAverage(ArrayList<String> words) {
47         int i = 0;
48         double sum = 0;
49         while (i < words.size()) {
50             double wordLength = (words.get(i).length());
51             sum = sum + wordLength;
52             i = i + 1;  
53         }
54         double average = sum / (words.size());
55         return average;
56     }
57 
58     private static void getLongWords(ArrayList<String> words, double average) {
59         System.out.println("The Longer Words Are... ");
60         int i = 0;
61         while (i < words.size()) {
62             String wordItem = words.get(i);
63             if (wordItem.length() > average) {
64                 System.out.println( wordItem);
65             } i= i+1;
66 
67         }
68     }
69 }
70     
71