Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program with concepts used through chapter 5 and meets the followin

ID: 3838949 • Letter: W

Question

Write a Java program with concepts used through chapter 5 and meets the following requirements:-

Write a Java program that prompts the user to enter a string Then displays the characters at odd position in the string. Count and display the number of vowels in the string.

Count and display the number of consonants in the string.

State what percentage of the String is vowels and what percentage is consonants

Be sure to use appropriate literals to describe what each line is displaying so anyone looking at the output will know exactly what the program is doing

Explanation / Answer

public class Count_vowelsandconsonants {

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

System.out.println("Enter file name: ");

File file = new File(new Scanner(System.in).next());

if (!file.isFile())

throw new FileNotFoundException(file + " is not a file");

int vowelCount = 0;

int consonantCount = 0;

try (BufferedReader in = new BufferedReader(new FileReader(file))) {

HashSet<Character> 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++;

else

consonantCount++;

}

}

}

} catch (IOException ex) {

}

System.out.println("Total vowels = " + vowelCount);

System.out.println("Total consonants = " + consonantCount);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote