I need to have the following program count the number of vowels in the text file
ID: 3628151 • Letter: I
Question
I need to have the following program count the number of vowels in the text file. I have copied my code below. When the program prints out the number of vowels, i just need the total number.import java.util.Scanner;
public class ReadData {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
// TODO code application logic here
// Create a File instance
java.io.File file = new java.io.File("scores.txt");
// Create a Scanner for the file
Scanner input = new Scanner(file);
// Read data from a file
while (input.hasNext()) {
String firstName = input.next();
String mi = input.next();
String lastName = input.next();
int score = input.nextInt();
System.out.println(
firstName + " " + mi + " " + lastName + " " + score);
}
input.close();
}
}
Explanation / Answer
please rate - thanks
import java.util.Scanner;
public class ReadData {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
// TODO code application logic here
// Create a File instance
java.io.File file = new java.io.File("scores.txt");
int count=0;
// Create a Scanner for the file
Scanner input = new Scanner(file);
// Read data from a file
while (input.hasNext()) {
String firstName = input.next();
String mi = input.next();
String lastName = input.next();
count+=countVowels(firstName);
count+=countVowels(mi);
count+=countVowels(lastName);
int score = input.nextInt();
System.out.println(
firstName + " " + mi + " " + lastName + " " + score);
}
System.out.println("There are "+count+" vowels in the file");
input.close();
}
public static int countVowels(String w)
{int i,count=0;
for(i=0;i<w.length();i++)
if(isvowel(w.charAt(i)))
count++;
return count;
}
public static boolean isvowel(char c)
{switch(c)
{case 'a': case 'A': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u':
return true;
default: return false;
}
}
}