In Java, String is represented as an Object.
String characters are represented in Unicode encoding.
In the English language, there are five vowels a, e, i, o, and u.
In any string, vowels can be in small or in large letters.
Here using the Java program we will see how to find vowels in the given string.
if (s.charAt(i) == ‘a’ || s.charAt(i) == ‘e’ || s.charAt(i) == ‘i’ || s.charAt(i) == ‘o’ || s.charAt(i) == ‘u’ || s.charAt(i) == ‘A’ || s.charAt(i) == ‘E’ || s.charAt(i) == ‘I’ || s.charAt(i) == ‘O’ || s.charAt(i) == ‘U’) {
System.out.println(“Found vowel ” + s.charAt(i) + ” at position ” + i);
}
Vowels program in java
Vowel program in java are discussed as follows
We created many programs to check and count vowels and consonants from String, StringBuffer, and from files.
Java program to print vowels in a String is as below
Java program to print vowels in a String
Steps to develop the program
- Take a String
- Create a loop to iterate each character of the string
- Inside loop check characters for vowel, both for upper and lower case
- If the character is a vowel then print its position and character
1 2 3 4 5 6 7 8 9 10 11 | public class VowelChecker { public static void main(String[] args) { String s = "Hello how are you WhAt is this"; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u' || s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I' || s.charAt(i) == 'O' || s.charAt(i) == 'U') { System.out.println("Found vowel " + s.charAt(i) + " at position " + i); } } } } |

In the above program, we created for loop and it runs up to string length.
charAt()
is used to access characters at specified index position.
1 2 3 4 5 6 7 8 9 10 | Found vowel e at position 1 Found vowel o at position 4 Found vowel o at position 7 Found vowel a at position 10 Found vowel e at position 12 Found vowel o at position 15 Found vowel u at position 16 Found vowel A at position 20 Found vowel i at position 23 Found vowel i at position 28 |
The above program is simple we can make some improvements to it.
We are comparing both uppercase and lower case, we can convert complete string in lower case and compare it will reduce all uppercase comparison.
How to find vowels in a string in java
write a java program to display all the vowels from a given string.
1 2 3 4 5 6 7 8 9 10 11 | public class VowelChecker { public static void main(String[] args) { String s = "Hello how are you WhAt is this"; String temp = s.toLowerCase(); for (int i = 0; i < temp.length(); i++) { if (temp.charAt(i) == 'a' || temp.charAt(i) == 'e' || temp.charAt(i) == 'i' || temp.charAt(i) == 'o' || temp.charAt(i) == 'u') { System.out.println("Found vowel " + temp.charAt(i) + " at position " + i); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 | Found vowel e at position 1 Found vowel o at position 4 Found vowel o at position 7 Found vowel a at position 10 Found vowel e at position 12 Found vowel o at position 15 Found vowel u at position 16 Found vowel a at position 20 Found vowel i at position 23 Found vowel i at position 28 |
count vowels in a string java Read from File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class VovelCounter { public static void main(String[] args) { Scanner sc = null; int count = 0; StringBuffer sb = new StringBuffer(); try { File file = new File("D://test.txt"); sc = new Scanner(file); } catch (FileNotFoundException ex) { System.out.println("File Not Found " + ex); } while (sc.hasNextLine()) { sb.append(sc.nextLine()); sb.append(System.lineSeparator()); } String s1 = sb.toString().toLowerCase(); for (int i = 0; i < s1.length(); i++) { if (s1.charAt(i) == 'a' || s1.charAt(i) == 'e' || s1.charAt(i) == 'i' || s1.charAt(i) == 'o' || s1.charAt(i) == 'u') { System.out.print(" "+s1.charAt(i)); count++; } } System.out.println(""); System.out.println("Vowel Count " + count); } } |
test.txt file is stored in d drive and file content is as below.
The Scanner class is used to read test.txt file.
1 2 3 4 5 6 | Java is a Object Oriented Programming Language Java is Simple Java is Secure Java is Robust Java is Multithreaded Java is Distributed |
1 2 | a a i a o e o i e e o a i a u a e a a i i e a a i e u e a a i o u a a i u i e a e a a i i i u e Vowel Count 48 |
Vowels program in java using switch case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.util.Scanner; public class VowelChecker1 { public static void main(String[] args) { System.out.println("Enter a character:"); Scanner sc = new Scanner(System.in); String s = sc.next(); char c = s.charAt(0); boolean isAlpha = Character.isAlphabetic(c); if (!isAlpha) { System.out.println("Enter a Alphabet"); System.exit(0); } switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': System.out.println("You Entered Vowel " + c); break; default: System.out.println("You Entered consonant " + c); } } } |
Read More: Switch Case in Java
Q: write a program to determine whether the input character is a vowel or consonant or not an alphabet
program to count the number of vowels and consonants in a given string in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.util.Scanner; public class VowelClass2 { public static void main(String[] args) { System.out.println("Enter a character:"); Scanner sc = new Scanner(System.in); String s = sc.next(); char[] vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; char c = s.charAt(0); boolean found = false; boolean isAlpha = Character.isAlphabetic(c); if (!isAlpha) { System.out.println("Enter a Alphabet Character"); System.exit(0); } for (char ch : vowels) { if (ch == c) { found = true; break; } } if (found) { System.out.println("Character is Vowel"); } else { System.out.println("Character is Consonant "); } } } |
Character class contains method isAlphabetic.
This method is checking whether a number is an alphabet or not.
If the number is alphabet then match the character using for each loop with vowel characters stored in the array.
If matched then the character is vowel else consonant.
Java program to replace vowels in a string
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.Scanner; public class VovelClass3 { public static void main(String[] args) { System.out.println("Enter a character:"); Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String newString = s.replaceAll("[aeiouAEIOU]", "@"); System.out.println("Your String is " + s); System.out.println("New String is " + newString); } } |
1 2 3 4 | Enter a character: Hello How are You Your String is Hello How are You New String is H@ll@ H@w @r@ Y@@ |
How do you print the number of vowels and consonants in a string?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.atom.basic; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class VowelnConsonantCount { public static void countVowelnCons(StringBuffer s) { int vowelCount = 0; int consonantCount = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u' || s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I' || s.charAt(i) == 'O' || s.charAt(i) == 'U') { vowelCount++; } else if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z' || s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') { consonantCount++; } } System.out.println("Total Vowels are " + vowelCount); System.out.println("Total Consonant are " + consonantCount); } public static void main(String args[]) { try { Scanner sc = new Scanner(new File("D:\\file.txt")); StringBuffer sb = new StringBuffer(); while (sc.hasNext()) { sb.append(sc.nextLine()); } countVowelnCons(sb); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
1 2 | Total Vowels are 111 Total Consonant are 224 |
Read More
- Duplicate Number between 1 to n numbers
- Sum of two numbers using command line arguments
- Program to reverse a string
- Prime number program in java using while loop
- Print vowels in a String
- Square Star Pattern
- Registration Form in Java | Student registration form using java swing source code