Vowel Count Write a program that reads a string from the use

Vowel Count

Write a program that reads a string from the user then determines and prints the number of vowels (uppercase and lowercase) in the string.

You will need:

An assignment statement to store the string value entered.

Two string methods – one to read each character, and another to return the length of the string. (Hint: Figure 3.1 in your text will be helpful.)

A for loop to cycle through the string. You MUST use a for loop.The loop should start at the first position (or index) in the string and end when the string is done (i.e. when you have reached the full length of the string.

Hint: what value is designated for the first position of a string?

Use the string method that returns the length as part of your condition.

In this loop you will need one switch statement. This switch statement will use both the ‘fall through method’ (for upper and lower case versions of the same vowel, as well as break statements (to separate one vowel from another.)

Use the string method that returns the character as the expression in your switch statement.

Output statements for the number of vowels.

Sample output:

Enter a string of characters:
Hello! I am Finn from the land of Ooo.

Number of each vowel in the string:
a: 2
e: 2
i: 2
o: 6
u: 0

Code Shell

//*********************************************************
// VowelCount.java       
//
// This program counts the number of vowels in a string
//*********************************************************

import java.util.Scanner;

public class VowelCount
{
   public static void main(String[] args)
   {
     /*declare variables to store the number
     of each type of vowel*/

     Scanner scan = new Scanner(System.in);

     //Get the string from the user (prompt and input)


     /* format for this part:
     
         for (initializaton; condition; update)
         {
             switch (exp)
             {
                 cases and statements go here
             
             
             }
             
         }
         
         Remember:
         1) condition should use method for getting string
           length
         2) exp (in switch) should use method for returning
           a character
     
     */

     //Output results
   }
}

Solution

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class VowelCount {


public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println(\"Enter a string of characters:\");
String line = input.nextLine();
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
  
map.put(\'a\', 0);
map.put(\'e\', 0);
map.put(\'i\', 0);
map.put(\'o\', 0);
map.put(\'u\', 0);
  
int start_index=0;
int end_index = line.length();
  
  
for(int i=start_index;i<end_index;i++)
{
switch(line.charAt(i))
{
case \'A\':
case \'a\':
map.put(\'a\', map.get(\'a\')+1);
break;
  
case \'E\':
case \'e\':
map.put(\'e\', map.get(\'e\')+1);
break;
  
case \'I\':
case \'i\':
map.put(\'i\', map.get(\'i\')+1);
break;
  
case \'O\':
case \'o\':
map.put(\'o\', map.get(\'o\')+1);
break;
  
case \'U\':
case \'u\':
map.put(\'u\', map.get(\'u\')+1);
break;
}
}
  
for(Map.Entry m:map.entrySet()){
System.out.println(m.getKey()+\" \"+m.getValue());
}
  
}
  
}

Vowel Count Write a program that reads a string from the user then determines and prints the number of vowels (uppercase and lowercase) in the string. You will
Vowel Count Write a program that reads a string from the user then determines and prints the number of vowels (uppercase and lowercase) in the string. You will
Vowel Count Write a program that reads a string from the user then determines and prints the number of vowels (uppercase and lowercase) in the string. You will

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site