JAVA Write a program that reads a file and counts the number
(JAVA) 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
- 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
The file reads as:
Example Dialog:
The file blah.txt has \"AEIOU\" in order 7 times
Solution
package pkg;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFileExample1 {
private static final String FILENAME = \"E:\\\\test\\\\filename.txt\";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
int count=0;
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
int a = 0, e = 0,i = 0,o = 0,u = 0;
String[] splited = sCurrentLine.split(\"(?!^)\");
for(int j = 0; j < sCurrentLine.length() ; j++){
if(splited[j].equals(\"A\") || splited[j].equals(\"a\") && a == 0)
a++;
else if(a>0){
if(a ==1){
if(splited[j].equals(\"E\") || splited[j].equals(\"e\"))
{
e++;
a--;
}
else if(splited[j].equals(\"A\") || splited[j].equals(\"a\"))
{
//e++;
a--;
}
}
else
a--;
}
else if(e>0){
if(e ==1){
if(splited[j].equals(\"I\") || splited[j].equals(\"i\"))
{
i++;
e--;
}
}
else
e--;
}
else if(i>0){
if(i ==1){
if(splited[j].equals(\"O\") || splited[j].equals(\"o\"))
{
o++;
i--;
}
}
else
o--;
}
else if(o>0){
if(o ==1){
if(splited[j].equals(\"U\") || splited[j].equals(\"u\"))
{
o--;
u++;
count++;
}
}
else
u--;
}
}
}
System.out.println(\"The file blah.txt has \\\"AEIOU\\\" in order \"+ count +\" times\");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
filename.txt


