Java The current code uses a hashset to count the amount of

 Java: The current code uses a hashset to count the amount of total vowels in my textfile.  How can I adjust it to count each vowel individually instead of the the total.  import java.io.*; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner;  public class Number2 {         static String fileName = \"E:\\\\Lincoln.txt\";          public static void main(String[] args) throws IOException {                  int vowelCount = 0;                  try (BufferedReader in = new BufferedReader(new FileReader(fileName))) {                          HashSet vowels = new HashSet<>(Arrays.asList(new Character[] { \'A\', \'E\', \'I\', \'O\', \'U\' }));                          String s;                         while ((s = in.readLine()) != null) {                                 for (char ch : s.toCharArray()) {                                         if (Character.isAlphabetic(ch)) {                                                 ch = Character.toUpperCase(ch);                                                 if (vowels.contains(ch))                                                         vowelCount++;                                         }                                 }                         }                  } catch (IOException ex) {                  }                  System.out.println(\"Total vowels = \" + vowelCount);         } } 

Solution

Number2.java

import java.io.*;

public class Number2 {
static String fileName = \"D:\\\\Lincoln.txt\";

public static void main(String[] args) throws IOException {

//int vowelCount = 0;
int letterACount = 0;
int letterBCount = 0;
int letterCCount = 0;
int letterDCount = 0;
int letterECount = 0;

try {
       BufferedReader in = new BufferedReader(new FileReader(fileName));
           String s;
while ((s = in.readLine()) != null) {
for (char ch : s.toCharArray()) {
if (Character.isAlphabetic(ch)) {
   ch = Character.toUpperCase(ch);
if(ch==\'A\'){
   letterACount++;
}
else if(ch==\'B\'){
   letterBCount++;
}
else if(ch==\'C\'){
   letterCCount++;
}
else if(ch==\'D\'){
   letterDCount++;
}
else if(ch==\'E\'){
   letterECount++;
}

}
}
}
System.out.println(\"Letter A Count: \"+letterACount);
System.out.println(\"Letter B Count: \"+letterBCount);
System.out.println(\"Letter C Count: \"+letterCCount);
System.out.println(\"Letter D Count: \"+letterDCount);
System.out.println(\"Letter E Count: \"+letterECount);
  

} catch (IOException ex) {

}

}
}

Output:

Letter A Count: 2
Letter B Count: 2
Letter C Count: 1
Letter D Count: 2
Letter E Count: 2

Input file:

abcdrfedghnikhgbtyoiae

 Java: The current code uses a hashset to count the amount of total vowels in my textfile. How can I adjust it to count each vowel individually instead of the t
 Java: The current code uses a hashset to count the amount of total vowels in my textfile. How can I adjust it to count each vowel individually instead of the t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site