Java Program to remove Vowels from the String

In this tutorial, we are going to learn java programming to remove all vowels present in the string. For any input string, we have to check whether the string contains vowels or not, if it does then remove the vowels and print the output.

For Example

Case1: If the user is given input ‘Quescol’

         The output should be ‘Qcsl’

         As we know that  ‘u’, ‘e’, and ‘o’ are vowels.

Case2: If the user is given input ‘Website’

         The output should be ‘Wbst’

         As we know that ‘e’, and ‘i’ are vowels

Java Program to Remove Vowel from Given String

import java.util.*;
public class Main
{
    public static void main (String[]args){
        ArrayList<Character> arr = new ArrayList<Character>(Arrays.asList('a','e','i','o','u','A','E','I','O','U'));
        System.out.println("Java program to remove Vowel from String");
        Scanner sc = new Scanner (System.in);
        System.out.println ("Please enter a String");
        String str = sc.nextLine(); 
        StringBuffer strBuffer = new StringBuffer();
        for(int i = 0; i < str.length(); i++){
            if(!arr.contains(str.charAt(i))) {  
                strBuffer.append(str.charAt(i));    
            }
        }
        System.out.println("After removing Vowel from String");
        System.out.println(strBuffer);
    }
}

Output

Java program to remove Vowel from String
Please enter a String
Quescol
After removing Vowel from String
Qscl

Explanation

As we can observe, for the input string ‘Quescol’ the output generated is ‘Qscl’. Where the vowels ‘u’, ‘e’, and ‘o’ are removed.

While iterating through the string, when vowels were found the vowels were replaced and the rest characters were concatenated and stored in strBuffer.