summaryrefslogtreecommitdiff
path: root/WordStats.java
blob: 22c3c7622e4fb9f23022318a679de082d6fcf8ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.*;

public class WordStats {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        String[] words = getListOfWords(console);
        System.out.println("Words: " + Arrays.toString(words));

        String longestWord = getLongestWord(words);
        System.out.println("Longest word: " + longestWord);
        
        vowelSearch(words);
    }
    

    /*
     * ask user for number of words to test, and store that (integer) as "number_of_Words"
     * loop for that number of iterations, adding each word to array "words"
     */
    public static String[] getListOfWords(Scanner console) {
        System.out.print("How many words? ");
        int numbers_of_WORDS = console.nextInt();

        String[] words = new String[numbers_of_WORDS];

        for (int i = 0; i < numbers_of_WORDS; i++) {
            System.out.print("Next word? ");
            String Word = console.next();
            words[i] = Word;
        }

        return words;
    }
    
    /*
     * iterate through array "words"
     * return longest word
     */

    public static String getLongestWord(String[] words) {
        String longestWord = "";
        for (int i = 0; i < words.length; i++) {
            String curr = words[i];
            if (curr.length() > longestWord.length()) {
                longestWord = curr;
            }
        }
        return longestWord;

    }

    /*
     * test first character of each word in array "words"
     * if first char is any defined in upper or lower case vowels, print it
     */
         
    public static void vowelSearch(String[] words) {
        char[] uppercaseVowels = { 'A', 'E', 'I', 'O', 'U' };
        char[] lowercaseVowels = { 'a', 'e', 'i', 'o', 'u' };
        System.out.println("Words beginning with vowels: ");
        for (int i = 0; i < words.length; i++) {
            String curr = words[i];
            char firstChar = curr.charAt(0);
            for (int j = 0; j < uppercaseVowels.length; j++) {
                if (firstChar == uppercaseVowels[j] || firstChar == lowercaseVowels[j]) {
                    System.out.println("    " + curr);
                }
            }
        }

    }


}