vowels program in java code example

Example 1: java program to print vowels in a string

String str = "hello";
String vowels = "aeiou";

for(char c : str) if(vowels.contains(c)) System.out.print(c + "");

Example 2: string vowels program in java

import java.util.Scanner;
import java.util.*;

class Test{
    
    public static boolean isVowel(char ch){
        if(ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U'){
            return true;
        }
        return false;
    }
    
    public static void main(String args[]){
        
        Scanner scan = new Scanner(System.in);
        List<String> arr = new ArrayList<String>();
        String str = scan.nextLine();
        String words[] = str.split("\\s");
        
        for(String k: words){
            boolean flag = false;
            boolean f1 = true;
            boolean f2 = true;
            if(k.length()<=2){
                continue;
            }
            for(int i = 0 ; i < k.length() ; i = i + 2){
                if(isVowel(k.charAt(i))==false){
                    f1 = false;
                    break;
                }
            }
            for(int i = 1 ; i < k.length() ; i = i + 2){
                if(isVowel(k.charAt(i))==false){
                    f2 = false;
                    break;
                }
            }
            if(f1 == true || f2 == true){
                flag = true;
            }
            if(flag == true){
                arr.add(k);
            }
        }
        if(arr.size()==0){
            System.out.print(-1);
        }
        else{
            for(int i = 0 ; i < arr.size() ; i++)
            {
                System.out.print(arr.get(i) + " ");
            }
        }
        scan.close();
    }
    
}

Tags:

Java Example