Java Program to Print Vowels in a String - Source Code

In this tutorial, I will show you how to count the number of Vowels in a sentence using a Java Source code. JOptionPane used here to beautify the project, but you can get a console output as well.

Step 1: Create VowelCount.java file in Eclipse (I used Eclipse as the Java IDE but you can use this file in any other Java IDEs as well)


//copyrighted by geekdecorders.blogspot.com
import javax.swing.JOptionPane;

public class VowelCount{

    public static void main(String args[]){
        String sentence=JOptionPane.showInputDialog("Enter Sentence:");
        int count = 0;
        for (int i = 0; i < sentence.length(); i++) {
            char c = sentence.charAt(i);
            if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
                count++;
            }
        }
        JOptionPane one = new JOptionPane();
        JOptionPane.showMessageDialog(one,"Vowel count is" + " " + count);
    }
}

Step 2: Run the program


Comments