Easy Way to Check Vowels in a String On Java

Introduction

In this task, we’ll write a Java program to check whether a vowel is present in a given string. Vowels in the English language are the letters ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’. This program will scan through each character of the string and determine if any of these vowels are present.

Java Program to Check for Vowels in a String

import java.util.Scanner;

public class VowelCheck {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scanner.nextLine().toLowerCase();

        boolean hasVowel = false;

        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                hasVowel = true;
                break;
            }
        }

        if (hasVowel) {
            System.out.println("The string contains a vowel.");
        } else {
            System.out.println("The string does not contain any vowels.");
        }
    }
}
Java

Explanation of the Steps

  1. User Input: The program starts by asking the user to input a string. This string is converted to lowercase using toLowerCase() to make the vowel check case-insensitive (i.e., it doesn’t matter whether the vowels are in uppercase or lowercase).
  2. Initialize the Flag: A boolean variable hasVowel is initialized to false. This flag will be set to true if a vowel is found in the string.
  3. Loop Through the String: The program then loops through each character in the string using a for loop. For each character, it checks if the character is one of the vowels (‘a’, ‘e’, ‘i’, ‘o’, or ‘u’).
  4. Check for Vowels: If a vowel is found, the hasVowel variable is set to true, and the loop is exited early using break because we only need to know if there is at least one vowel.
  5. Output the Result: After the loop, the program checks the value of hasVowel. If it’s true, the program prints that the string contains a vowel. Otherwise, it prints that the string does not contain any vowels.

How the Output is Displayed

  1. User Enters a String: The program prompts the user to enter a string.
  2. Program Checks for Vowels: The program processes the string character by character to check for the presence of vowels.
  3. Displays Result: Finally, based on whether a vowel is found or not, the program displays a message to the user.

Example Output

Example 1:

Result

Enter a string: hello
The string contains a vowel.
Java

Example 2

Result

Enter a string: sky
The string does not contain any vowels.
Java

Explanation of the Output

  • Example 1: The user enters the string “hello”. The program detects the vowels ‘e’ and ‘o’ and displays the message “The string contains a vowel.”
  • Example 2: The user enters the string “sky”. The program does not find any vowels in this string, so it displays “The string does not contain any vowels.”

Conclusion

This simple Java program effectively checks if a string contains any vowels by scanning each character and comparing it to the set of vowels. This exercise helps in understanding string manipulation, character comparison, and basic control structures like loops and conditional statements in Java.

References for Learning Java

If you want to learn more about Java and enhance your programming skills, here are some excellent free resources:

  1. Oracle Java Tutorials
    The official Java tutorials from Oracle, covering everything from the basics to advanced topics.
  2. GeeksforGeeks Java
    Comprehensive tutorials and practice problems, ideal for learners at all levels.
  3. W3Schools Java Tutorial
    Simple and easy-to-understand tutorials for beginners, with interactive examples.
  4. Codecademy Learn Java
    An interactive platform that allows you to write and test Java code directly in your browser.
  5. Coursera Java Courses
    Offers various free and paid Java courses taught by university professors and industry experts.

These resources provide a solid foundation for learning Java and improving your programming skills.

Please rate our website(required)

Leave a Reply

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

Resize text
Scroll to Top