Java program to print the vowels in a string:
This Java program will print all the vowels in a string. With this example, you will learn how to iterate over the characters of a string, how to check if a character is vowel or not and how to print all the vowels.
I will show you two different ways to write this program.
Method 1: Iterate and find the vowels:
With this example, the program will iterate over the characters and find out all vowels of the string. It will print these vowels one by one.
import java.util.Scanner;
class Main {
private static boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
|| c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = scanner.nextLine();
System.out.println("Vowels found: ");
for (char c : str.toCharArray()) {
if (isVowel(c)) {
System.out.print(c + " ");
}
}
}
}
Here,
- The isVowel method is used to check if a character is a vowel or not. It takes a character as its parameter and returns one boolean value. It compares the character with both uppercase and lowercase vowels. It returns true if the character c is a vowel and false otherwise.
- The scanner object reads the user input. It reads the user input string and assigns it to the str variable.
- The for loop iterates over the character of the string one by one and it calls the isVowel method to check if the current character is a vowel or not. If it is a vowel, it prints the character.
If you run this program, it will print output as below:
Enter a string:
Hello World
Vowels found:
e o o
Enter a string:
Hello Universe
Vowels found:
e o U i e e
Method 2: Remove duplicate characters:
In the above example, it prints all the characters found. It doesn’t check if the same character was printed before. We can use a HashSet to remove duplicate characters. Let me change the above example to use HashSet:
import java.util.HashSet;
import java.util.Scanner;
class Main {
private static boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
|| c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
HashSet<Character> characterSet = new HashSet<>();
System.out.println("Enter a string: ");
String str = scanner.nextLine();
System.out.println("Vowels found: ");
for (char c : str.toCharArray()) {
if (isVowel(c)) {
characterSet.add(c);
}
}
characterSet.forEach(c -> System.out.print(c + " "));
}
}
We made a few changes to the program.
- One HashSet object is created to add the vowels.
- In the for loop, the vowels are added to the HashSet. The HashSet class provides a method called add() to add an item. It will not add any duplicate characters.
- The last line is iterating over the HashSet and printing its content.
You might also like:
- 3 ways in Java to print a reverse half pyramid pattern
- Java program to find the largest and smallest of three numbers
- Java vector addAll method explanation with example
- Java program to find the largest and smallest of n user input numbers
- Java program to print left-aligned and right-aligned staircase patterns
- 4 ways in Java to convert a decimal value to octal
- Java program to round a number to n decimal places