I need help in writing a Java program that will ask the user

I need help in writing a Java program that will ask the user for a phrase as an input (use the Scanner class method .nextLine() to read in a phrase). I need it to then print to the screen the number of vowels (a or e or i or o or u) in the phrase and the number of consonants (letters). So if I enter the phrase “Crummy class grumble moan” it will print out :

Crummy class grumble moan

Has a total of 6 vowels and 16 consonants.

Solution

ReadSentence.java


public class ReadSentence {

  
   public static void main(String[] args) {
       java.util.Scanner scanner = new java.util.Scanner(System.in);
       System.out.println(\"Enter a sentence: \");
       String sentence = scanner.nextLine();
       int vowels = 0;
       int consonants = 0;
       for(int i=0; i<sentence.length(); i++){
           char ch = sentence.charAt(i);
           if(ch == \'a\'||ch==\'e\'||ch==\'i\'||ch==\'o\'||ch==\'u\'){
               vowels++;
           }
           else if(ch != \' \'){
               consonants++;
           }
       }
       System.out.println(sentence+\" Has a total of \"+vowels+\" vowels and \"+consonants+\" consonants.\");
   }

}

Output:

Enter a sentence:
Crummy class grumble moan
Crummy class grumble moan Has a total of 6 vowels and 16 consonants.

I need help in writing a Java program that will ask the user for a phrase as an input (use the Scanner class method .nextLine() to read in a phrase). I need it

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site