Write a program that asks the user for a word or phrase and
Solution
package org.students;
import java.util.Scanner;
public class VowelsCount {
public static void main(String[] args) {
//Scanner class object us used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Declaring variables
int vowelCount=0;
//This loop continues to execute until the user enters either \'q\' or \"quit\"
while(true)
{
//Getting the string entered by the user
System.out.print(\"Enter the String :\");
String str=sc.nextLine();
//Check whether the user entered string is \"q\" or \"quit\"
if(str.equals(\"q\") || str.equals(\"quit\"))
{
System.out.println(\":: Program Exit ::\");
break;
}
else
{
//calling the method by passing the user entered string as argument
vowelCount=countVowels(str);
//displaying the no of vowels in the string
System.out.println(\"No of vowels in the String are :\"+vowelCount);
}
}
}
//this method will find the no of vowels in the string
private static int countVowels(String str) {
//Declaring variables
int vowelCount=0;
//this for loop will count the number of vowels
for (int i=0; i<str.length(); ++i)
{
if(str.charAt(i)==\'a\'||str.charAt(i)==\'e\'||str.charAt(i)==\'i\'||str.charAt(i)==\'o\'||str.charAt(i)==\'u\'
||str.charAt(i)==\'A\'||str.charAt(i)==\'E\'||str.charAt(i)==\'I\'||str.charAt(i)==\'O\'||str.charAt(i)==\'U\')
{
//counting the number of vowels
vowelCount++;
}
}
return vowelCount;
}
}
___________________________
Output:
Enter the String :hello
No of vowels in the String are :2
Enter the String :how are you mary
No of vowels in the String are :6
Enter the String :q
:: Program Exit ::
_________Thank You

