In this post, we will see a Java program to print vowels in a String. If a String contains any vowels, then we will display those vowels to the user.
Generally, the English alphabet contains 5 vowels a, e, i, o, and u. In programming also, we will find those vowels and print them. So to do this, we have to write the logic for extracting these literals from a String.
Let’s understand the hidden logic here.
Logic to find vowels in a String
Let’s think.. How we can extract any character from a String in a program?
Simply, we can use charAt( ) method, right? Yes, we can use charAt( ) method.
Ok, the next question is how can we get those characters in a String? Simply, If we will iterate String in ascending order or descending order, then easily we can find those characters one by one and to iterate we can use any iterative loops like for loop or while loop.
That’s it!
We can print those characters. We got all vowels!
Piece Of Code

Algorithm
Accept any String from the user.
Convert given String to lowercase String.
Start for loop from '0' to str.length( )-1
Declare if condition to check whether the current character is vowel or not
Display vowels of given String.
Code language: Java (java)
Program-1: Java Program to print vowels in a String
In this program, we will see how to find vowels in a String. We will use all vowels directly with for loop and if condition to check them.
Let’s see the program now.
package cf.java.string;
import java.util.Scanner;
public class JavaProgramToPrintVowelsInAString {
public static void main(String[] args) {
// Declare a variables
String str = null;
Scanner sc = new Scanner(System.in);
// Accept any string from user
System.out.print("Enter any String: ");
str = sc.nextLine();
// Convert String to lower case letter
str = str.toLowerCase();
// Print all vowels
System.out.print("Vowels in the given String are:");
// For loop to iterate String
for (int i = 0; i < str.length(); i++) {
// Check a character is vowel or not
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
|| str.charAt(i) == 'u') {
// Display each vowel
System.out.print(" " + str.charAt(i));
}
}
}
}
Code language: PHP (php)
Output:
Enter any String: Codingface
Vowels in the given String are: o i a e
Code language: JavaScript (javascript)
Also Read: Palindrome program in Java for both String and Number.
Program-2: Java Program to count the number of vowels & consonants in a String
In the first program, We have already seen that how we can write a Java program to print vowels in a String. Let’s see another program to count the number of vowels and consonants in a String.
In the below program, we will count both vowels and consonants of String and we will display the count to the user.
package cf.java.string;
import java.util.Scanner;
public class JavaProgramToCountVowelsAndConsonants {
public static void main(String[] args) {
// Declare a variables
String str = null;
int vCount=0,cCount=0;
Scanner sc = new Scanner(System.in);;
// Accept any string from user
System.out.print("Enter any String: ");
str = sc.nextLine();
// Convert String to lowercase letter
str = str.toLowerCase();
// For loop to iterate String
for (int i = 0; i < str.length(); i++) {
// Check a character is vowel or not
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
|| str.charAt(i) == 'u') {
// Increment vowel count by one
vCount++;
}
// Check if character is a consonant or not
else if(str.charAt(i)>='a' && str.charAt(i)<='z'){
// Increment consonant count by one
cCount++;
}
}
System.out.println("No of vowels given in String: "+vCount);
System.out.println("No of consonant given in String: "+cCount);
}
}
Code language: Java (java)
Output:
Enter any String: Codingface
No of vowels given in String: 4
No of consonant given in String: 6
Code language: JavaScript (javascript)
People Also Search For:
- Write a Java program to print vowels in a String.
- Java program to find vowels in a string
- Program to print vowels in a string in Java
- Program to check vowel in Java
- Vowels program in Java using Scanner
- Write a program to calculate the number of vowels present in the string
- Java program to count number of vowels and consonants in a string
FAQs
How do you print a vowel in a String?
By using the charAt( ) method and for loop or while loop, we can compare with all vowels i.e. a, e, i, o, u, and when we will find any of them, we can print that vowel.
Can we use the same English alphabet vowels in Java?
Yes, we can use the same 5 English alphabet vowels a, e, i, o, and u in Java also.
Which Iterator we can use to print vowels in a String?
We can use any iterators like for loop or while loop to print vowels in a String.
3 thoughts on “Java Program To Print Vowels In A String – 2 Simple Programs”