VowelCounter.java

1 minute read

This program was created to count the number of vowels in a word.

Once again, it uses the dreadful and outdated Java GUI named JOptionPane to take user input check each letter of the string and returns how many vowels there are.

The code has been modified slightly and now uploaded to the site.

<— Return Home

JOptionPane Code Snippet

/*
Author: ENTPRESTIGIOUS
Compiler: JDK 11.0.10 LTS
Link: https://edabit.com/challenge/GBKphScsmDi9ek3ra
*/
    
import javax.swing.JOptionPane;
public class VowelCounter {
    public static void main(String[] args){
        JOptionPane.showMessageDialog(null, "Welcome!\nPress enter to continue...");
        boolean looper = true;
        while (looper == true){
            String input = JOptionPane.showInputDialog(null, "Please type a word or phrase to determine how many vowels! Press X on top right to exit!");
            int result = getCount(input);
            JOptionPane.showMessageDialog(null, "The number of vowels in this word or phrase is: " + result);
        }
    }

    public static int getCount(String str){
        int vowelCounter = 0;
        String big = str.toUpperCase();
        char[] newArray = new char[str.length()];
        int i = 0;
        for (i = 0; i < newArray.length; i++){
            char separate = big.charAt(i);
            newArray[i] = separate;
            //JOptionPane.showMessageDialog(null, newArray[i]);
            if (newArray[i] == 'A' || newArray[i] == 'E' || newArray[i] == 'I' || newArray[i] == 'O' || newArray[i] == 'U'){
                vowelCounter++;
            }
        }
        return vowelCounter;
    }
}

<— Return Home