This repository has been archived on 2026-03-18. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
CSE-122/WordStats.java
2026-03-18 00:39:22 -07:00

76 lines
2.2 KiB
Java

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);
}
}
}
}
}