Slip No 17 Q A

Write a java program to accept a String from user and display each vowel from a String after 3 seconds.

import java.util.Scanner;

public class VowelDelay {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String input = sc.nextLine();

        System.out.println("Vowels in the string (displayed after 3 seconds each):");

        // Convert string to lowercase to handle both cases
        String str = input.toLowerCase();

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);

            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                System.out.println(ch);

                try {
                    // Pause for 3 seconds
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    System.out.println("Thread interrupted: " + e);
                }
            }
        }

        sc.close();
        System.out.println("All vowels displayed.");
    }
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top