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

I need help figuring out how to add Finding the Vowels and Consonants of the use

ID: 3812718 • Letter: I

Question

I need help figuring out how to add Finding the Vowels and Consonants of the user input. I am doing the program in DrJava if that makes a difference. I've added a link with an image of the assignment here:

http://imgur.com/a/dUW23

Thanks!


import java.util.Scanner;
import java.io.*;
public class WordAnalyzerImproved{
public static void main (String [] args) throws IOException{
  
Scanner cin = new Scanner(System.in);

System.out.println("Welcome to the Amazing Word Analyzer!: ");


System.out.println("Enter a word");
  

String word = cin.next();
String word1 = cin.next();
String word2 = cin.next();
System.out.println("You entered: " + word);

String str;
int vowels = 0;
int vowels1 = 0;
int vowels2 = 0;
char ch;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter your first word or END to end the program: ");
//print out first letter of the word
System.out.println("The first letter is: " + word.charAt(0) );
//print out the second letter of the word
System.out.println("The second letter is: " + word.charAt(1) );
//print out the third letter of the word
System.out.println("The third letter is: " + word.charAt(2) );

   str = br.readLine();
   for(int i = 0; i < str.length(); i ++)
   {
     ch = str.charAt(i);

   if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||
   ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
    vowels ++;

   }
   System.out.println("Vowels : " + vowels);



System.out.println("Enter a word");
System.out.println("You entered: " + word1);

System.out.println("Please enter your second word or END to end the program: ");
   //print out first letter of the word
System.out.println("The first letter is: " + word1.charAt(0) );
//print out the second letter of the word
System.out.println("The second letter is: " + word1.charAt(1) );
//print out the third letter of the word
System.out.println("The third letter is: " + word1.charAt(2) );


   str = br.readLine();
   for(int i = 0; i < str.length(); i ++)
   {
     ch = str.charAt(i);

   if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||
   ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
    vowels ++;

   }
System.out.println("Vowels : " + vowels1);

/*
System.out.println("Enter a word");
System.out.println("You entered: " + word2);

System.out.println("Please enter your third word or END to end the program: ");
//print out first letter of the word
System.out.println("The first letter is: " + word2.charAt(0) );
//print out the second letter of the word
System.out.println("The second letter is: " + word2.charAt(1) );
//print out the third letter of the word
System.out.println("The third letter is: " + word2.charAt(2) );
//print out the fourth letter of the word
System.out.println("The fourth letter is: " + word2.charAt(3) );

   */
String words = cin.nextLine();
int count = 3;
double sum = 3;
double average = 3;

cin = new Scanner (words);

while (cin.hasNext()){
   String userInput = cin.next();

   double charNum = userInput.length();
   sum = charNum + sum;
   count++;

   if (count > 3){
     average = sum/ count;
   }
}
System.out.println("You entered three words with an average word length of = " +average);
    }
}

Explanation / Answer

package org.students;

import java.util.Scanner;

public class WordAnalyzerImproved {

   public static void main(String[] args) {
       // Declaring variables
       String word;
       int count = 0, totWordlen = 0;
       int vowels, consonents, totVowels = 0, totConsonents = 0;

       // Scanner object is used to get the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       System.out.print("Welcome to amazing word Analyzer ");

       // Getting the word entered by the user
       System.out
               .print("Please enter the first word (or END) to end the program :");
       word = sc.next();
       if (word.equals("END")) {
           System.out.println("You did not enter any words!");
       } else {
           // This loop continues to execute until the user entered the word
           // "END"
           while (true) {
               if (!word.equals("END")) {
                   // Calculating the total length of all words for every time
                   // when the use entered
                   totWordlen += word.length();

                   // counting no of words entered by the user
                   count++;

                   vowels = 0;
                   consonents = 0;

                   // checking the character of the word is vowel or consonant
                   for (int i = 0; i < word.length(); i++) {
                       if (word.charAt(i) == 'a' || word.charAt(i) == 'e'
                               || word.charAt(i) == 'i'
                               || word.charAt(i) == 'o'
                               || word.charAt(i) == 'u'
                               || word.charAt(i) == 'A'
                               || word.charAt(i) == 'E'
                               || word.charAt(i) == 'I'
                               || word.charAt(i) == 'O'
                               || word.charAt(i) == 'U') {
                           // Counting the vowels in the word
                           vowels++;

                       } else {
                           // Counting the consonants in the word
                           consonents++;
                       }
                   }
                   // calculating total vowels and total consonants
                   totVowels += vowels;
                   totConsonents += consonents;

                   // displaying message
                   System.out.println("The word " + word + " has " + vowels
                           + " vowel and " + consonents + " consonents");

                   // getting the word entered by the user
                   System.out
                           .print(" Please enter the next word (or END) to end the program :");
                   word = sc.next();
                   continue;
               } else
                   break;
           }

           // displaying average no of words entered by the user
           System.out.println("You entered " + count
                   + " words with an average word length of "
                   + (double) totWordlen / count);

           // Displaying total no of vowels and consonants of all words
           System.out
                   .println("The Total Vowels for all words is " + totVowels);
           System.out.println("The Total Consonents for all words is "
                   + totConsonents);
       }

   }

}

___________________

Output#1:

Welcome to amazing word Analyzer
Please enter the first word (or END) to end the program :Cat
The word Cat has 1 vowel and 2 consonents

Please enter the next word (or END) to end the program :Dog
The word Dog has 1 vowel and 2 consonents

Please enter the next word (or END) to end the program :Goat
The word Goat has 2 vowel and 2 consonents

Please enter the next word (or END) to end the program :END
You entered 3 words with an average word length of 3.3333333333333335
The Total Vowels for all words is 4
The Total Consonents for all words is 6

_________________

Output#2:

Welcome to amazing word Analyzer
Please enter the first word (or END) to end the program :END
You did not enter any words!

______________Thank You

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