Objective: Write a program that reads a file and counts the number of times the
ID: 3766947 • Letter: O
Question
Objective:
Write a program that reads a file and counts the number of times the vowels ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ occurs exactly in that order.
It ignores consonants
It ignores any type of space
It ignores case
The only thing it cannot ignore is if another vowel occurs out of order
These count:
AEIOU
aeiou
hahehihohu
Take it out
These do not count:
AEIuO
Taco is good
Take it over
Hints and Tips:
It may be a good idea to build one long string and use .charAt to examine each loop
There may be many nested loops
Example Dialog:
The file blah.txt has "AEIOU" in order 8 times
The file:
Explanation / Answer
Oh wow. Thats a nice hint. ;) Anyways, here is the code for you. If you have any further queries, just get back to me.
import java.util.*;
import java.io.*;
class ReadVowelsFromFile
{
public static void main(String[] args) throws IOException
{
String entireFileText = new Scanner(new File("blah.txt")).useDelimiter("\A").next();
entireFileText = entireFileText.toLowerCase();
int count = 0;
String vowels = "";
for(int i = 0; i < entireFileText.length(); i++)
{
if(vowels == "" && entireFileText.charAt(i) == 'a')
vowels = "a";
else if(vowels == "a" && entireFileText.charAt(i) == 'e')
vowels = "ae";
else if(vowels == "ae" && entireFileText.charAt(i) == 'i')
vowels = "aei";
else if(vowels == "aei" && entireFileText.charAt(i) == 'o')
vowels = "aeio";
else if(vowels == "aeio" && entireFileText.charAt(i) == 'u')
{
vowels = "";
count++;
}
}
System.out.println("The file blah.txt has AEIOU in order "+count+" times");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.