Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class Consonants
- {
- public static void main(String[] args)
- {
- String consonants = "bcdfghjklmnpqrstvwxyz"; //List of consonants
- String vowels = "aeiou"; //List of vowels
- String pattern = "[a-zA-Z]+";
- String result = "";
- String userInput = JOptionPane.showInputDialog(null, "Please enter a word to find the consonant content.",
- "Consonant Content", 2); //This gets a word from the end user
- if (userInput != null & userInput.matches(pattern)) //If statement will execute if the end user gives some valid input
- {
- userInput = userInput.toLowerCase(); //Makes everything lower case, just to make comparing easier
- int[] intArray = new int[userInput.length()];
- String[] stringArray = new String[userInput.length()];
- String placeHolder = "";
- for (int i = 0; i < userInput.length(); i++) //Gets a letter of the word given by the user
- {
- placeHolder = userInput.substring(i, i + 1);
- for (int j = 0; j < consonants.length(); j++) //Get a letter in the consonants string
- {
- if (placeHolder.equals(consonants.substring(j, j + 1))) //Compares the two letters
- {
- stringArray[i] = userInput.substring(i, i + 1);
- intArray[i]++;
- }
- }
- }
- for(int i = 0; i < stringArray.length; i++)
- result += "\n" + stringArray[i] + "\t" + intArray[i];
- }
- else
- result = "Invalid input.";
- JOptionPane.showMessageDialog(null, result, "Consonant Content", 2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment