SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Flow Of Control
Exercise:
Programming Projects
Question:14 | ISBN:9780132830317 | Edition: 5

Question

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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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.");

 

}

 

}

0 0

Discussions

Post the discussion to improve the above solution.