Count Number of Vowels in Java GUI

In this tutorial, i will teach you how to create a program that counts how many vowels are inputted in a text using java that has a GUI interface. Now, let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of Vowels.java. 2. Import javax.swing package. Hence we will use a GUI (Graphical User Interface) here like the inputting the word.
  1. import javax.swing.*;
3. Before coding in the Main, create first a function named countVowels as integer and a text variable as string as parameters for this function.
  1. static int countVowels(String text) {
  2.     int count = 0; // start the count at zero
  3.     // change the string to lowercase
  4.     text = text.toLowerCase();
  5.   //gets the vowel characters
  6.     for (int i = 0; i < text.length(); i++) {
  7.         char c = text.charAt(i);
  8.         if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
  9.             count++;
  10.         }
  11.     }
  12.     return count; //returns the number of vowels inputted
  13. }
4. Initialized your variable in your Main, variable Str as string and make it as an input and an numVowels as Integer as a variable for finding and displaying the number of vowels.
  1.         String Str;        
  2.         int numVowels;      
  3.  
  4.         Str = JOptionPane.showInputDialog(null, "Enter String:");
5. Count the number of strings using the countVowels function that will hold the Str variable as an input and will be held by the numVowels variable.
  1. numVowels = countVowels(Str);
6. Lastly, display the output of the program using JOptionPane.showMessageDialog to count how many vowels did the input string has.
  1. JOptionPane.showMessageDialog(null, numVowels + " vowel/s detected from the input!");
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*;
  2.  
  3. public class Vowels {
  4.  
  5.  
  6.     public static void main(String[] args) {
  7.    
  8.         String Str;        
  9.         int numVowels;      
  10.  
  11.         Str = JOptionPane.showInputDialog(null, "Enter String:");
  12.        
  13.         numVowels = countVowels(Str);                          
  14.  
  15.         JOptionPane.showMessageDialog(null, numVowels + " vowel/s detected from the input!");
  16.     }
  17.    static int countVowels(String text) {
  18.     int count = 0;
  19.     text = text.toLowerCase();
  20.  
  21.     for (int i = 0; i < text.length(); i++) {
  22.         char c = text.charAt(i);
  23.         if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
  24.             count++;
  25.         }
  26.     }
  27.     return count;
  28. }
  29.  
  30. }
Hope this helps! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment