/home/sjenks/NetBeansProjects/CS2/src/review/NonextremeWords.java
 1 /*
 2  * Write a program to prompt user for a puncuation free sentence consisting of
 3  * at least a few words from the standard imput stream and print the words in 
 4  * the sentence, other than that which comes frist alphabetically and that which
 5  * comes last alphabetically in the order in which they wre presented on the 
 6  * input stream, one word per line. 
 7  */
 8 package review;
 9 
10 import java.util.ArrayList;
11 import java.util.Scanner;
12 
13 /**
14  *
15  * @author sjenks
16  */
17 public class NonextremeWords {
18 
19     /**
20      * @param args the command line arguments
21      */
22     public static void main(String[] args) {
23         ArrayList<String> words = getWords();
24         String firstAlphabetically = getChampion(words);
25         String lastAlphabetically = getLast(words);
26         words.remove(firstAlphabetically);
27         words.remove(lastAlphabetically);
28         words.forEach(i->System.out.println(i));
29     }
30 
31     private static ArrayList<String> getWords() {
32         ArrayList<String> words = new ArrayList<>();
33         Scanner scanner = new Scanner(System.in);
34         System.out.print("Sentence? ");
35         String sentence = scanner.nextLine();
36         Scanner reader = new Scanner(sentence);
37         while (reader.hasNext()) {
38             String word = reader.next();
39             words.add(word);
40         }
41         return words;
42 
43     }
44 
45     private static String getChampion(ArrayList<String> words) {
46        String champion = words.get(0);
47        int i = 1;
48        while (i< words.size()){
49            String challenger = words.get(i);
50            
51            if (champion.compareToIgnoreCase(challenger)> 0){
52                //if challenger comes before champion
53                champion= challenger;               
54            }
55            i=i+1;
56        }
57         return champion;
58     }
59 
60     private static String getLast(ArrayList<String> words) {
61         String last = words.get(0);
62         int i = 1;
63         while (i < words.size()) {
64             String challenger = words.get(i);
65 
66             if (last.compareToIgnoreCase(challenger) < 0) {
67                 //if last comes before challenger
68                 last = challenger;
69             }
70             i = i + 1;
71         }
72         return last;
73 
74     }
75 }
76