The file words.txt on the book’s website contains 87,314 words from the English language. Write a program that reads through this file and finds the word that has the most consecutive vowels. For example, the word "bedouin" has three consecutive vowels.
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class CH3_PP14 {
public static void main(String[] args) {
// Declaring variables
Scanner inFile = null;
String name , consecutiveWord = null;
int concecutiveVovels = 0;
int maxConsecutiveVovels = 0;
/*
* Using try/catch blocak to handle file not found exception while opening a text file "word.txt". *
*/
try
{
inFile = new Scanner(new FileInputStream("words.txt"));
} catch (FileNotFoundException e) {
System.out.println("File not found.");
System.exit(0);
}
while(inFile.hasNext())
{
name = inFile.nextLine();
for (int i = 0; i < name.length(); i++ )
{
/* Check whether the consecutive characters are vowels
* then increment the consecutive numbers by one.
*
*/
if (name.charAt(i) =='a' || name.charAt(i) == 'e' || name.charAt(i) == 'o'|| name.charAt(i) == 'u' || name.charAt(i)== 'i')
{
concecutiveVovels++;
} else {
if (concecutiveVovels > maxConsecutiveVovels)
{
maxConsecutiveVovels = concecutiveVovels;
consecutiveWord = name;
}
concecutiveVovels = 0;
}
}
if(concecutiveVovels > maxConsecutiveVovels)
{
maxConsecutiveVovels = concecutiveVovels;
consecutiveWord = name;
}
}
inFile.close();
if (consecutiveWord != null)
{
System.out.println("The maximum consecutive vowel string: " + consecutiveWord);
System.out.println("Vovel count: " + maxConsecutiveVovels);
} else
System.out.println("No String found.");
}
}