Question: I need to write a program in Java 8 that counts the consonants and vowels of a String. Unfortunately, my program also counts the spaces

I need to write a program in Java 8 that counts the consonants and vowels of a String. Unfortunately, my program also counts the spaces in between letters and gives me the wrong output. Can you fix this problem? This is what I have done so far:

import java.util.Scanner;

public class Tri {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.println(" Enter a String of Letters: ");

String str = input.nextLine();

str = str.toUpperCase().trim();

int vcount = 0;

int ccount = 0;

for ( int i = 0; i < str.length(); i++) {

char ch = str.charAt(i);

if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

vcount++;

else if (Character.isLetter(ch));

ccount++;

}

System.out.print("Vowels occur" + vcount + "times and consonants occur" + ccount + "times");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!