Thursday, March 20, 2025

Java: Extracting Vowels in Order from a Sentence


Let's dive into a simple yet practical Java program that extracts all vowels from a given sentence and prints them in the order they appear. This can be a handy little script for text processing or just a fun way to explore string manipulation in Java.

Understanding the Logic

Our approach will involve these steps:

 * Input: Take a sentence as input (either hardcoded or from user input).

 * Iteration: Loop through each character of the sentence.

 * Vowel Check: For each character, check if it's a vowel (a, e, i, o, u, and their uppercase counterparts).

 * Output: If a character is a vowel, print it immediately.

Java Code

Here's the Java code that implements this logic:

public class VowelExtractor {


    public static void main(String[] args) {

        String sentence = "This is a sample sentence with various vowels.";


        System.out.println("Vowels in the sentence:");

        printVowels(sentence);

    }


    public static void printVowels(String sentence) {

        if (sentence == null || sentence.isEmpty()) {

            System.out.println("Empty sentence or null");

            return;

        }


        for (int i = 0; i < sentence.length(); i++) {

            char ch = sentence.charAt(i);

            if (isVowel(ch)) {

                System.out.print(ch); // Print the vowel without a newline

            }

        }

        System.out.println(); // Add a newline at the end

    }


    public static boolean isVowel(char ch) {

        ch = Character.toLowerCase(ch); // Convert to lowercase for easier comparison

        return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';

    }

}


Explanation

 * main(String[] args):

   * This is the entry point of our program.

   * We define a sample sentence.

   * We call the printVowels method, passing the sentence as an argument.

 * printVowels(String sentence):

   * This method iterates through each character of the input sentence.

   * It handles null or empty input strings.

   * For each character, it calls the isVowel method to check if it's a vowel.

   * If isVowel returns true, the character is printed using System.out.print(ch). Note that print is used to print characters on the same line.

   * System.out.println() adds a new line at the end of the printed vowels.

 * isVowel(char ch):

   * This method checks if a given character is a vowel.

   * It converts the character to lowercase using Character.toLowerCase(ch) to simplify the comparison.

   * It returns true if the character is one of 'a', 'e', 'i', 'o', or 'u'; otherwise, it returns false.

How to Run the Code

 * Save: Save the code as VowelExtractor.java.

 * Compile: Open a terminal or command prompt and navigate to the directory where you saved the file. Then, compile the code using the command: javac VowelExtractor.java

 * Run: After successful compilation, run the program using: java VowelExtractor

Output

The output for the given sample sentence will be:

Vowels in the sentence:

iiaaeeeiouaeiou


Enhancements

 * User Input: You can modify the main method to take the sentence as user input using the Scanner class.

 * Storing Vowels: Instead of printing the vowels directly, you could store them in a List or StringBuilder for further processing.

 * Handling Y: You could modify the isVowel method to include 'y' as a vowel in certain contexts (e.g., at the end of a word).

 * Removing Duplicates: You could add logic to remove duplicate vowels from the output.

 * Counting Vowels: You can add a counter to keep track of the number of vowels found.

This basic program provides a foundation for more complex text processing tasks in Java. Feel free to experiment and enhance it based on your specific requirements.


This Content Sponsored by Buymote Shopping app

BuyMote E-Shopping Application is One of the Online Shopping App

Now Available on Play Store & App Store (Buymote E-Shopping)

Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8

Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication


No comments:

Post a Comment