Guest User

javahelp

a guest
Feb 7th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. public class Consonants
  3. {
  4. public static void main(String[] args)
  5. {
  6. String consonants = "bcdfghjklmnpqrstvwxyz"; //List of consonants
  7. String vowels = "aeiou"; //List of vowels
  8. String pattern = "[a-zA-Z]+";
  9. String result = "";
  10.  
  11. String userInput = JOptionPane.showInputDialog(null, "Please enter a word to find the consonant content.",
  12. "Consonant Content", 2); //This gets a word from the end user
  13.  
  14. if (userInput != null & userInput.matches(pattern)) //If statement will execute if the end user gives some valid input
  15. {
  16. userInput = userInput.toLowerCase(); //Makes everything lower case, just to make comparing easier
  17. int[] intArray = new int[userInput.length()];
  18. String[] stringArray = new String[userInput.length()];
  19.  
  20. String placeHolder = "";
  21.  
  22. for (int i = 0; i < userInput.length(); i++) //Gets a letter of the word given by the user
  23. {
  24. placeHolder = userInput.substring(i, i + 1);
  25. for (int j = 0; j < consonants.length(); j++) //Get a letter in the consonants string
  26. {
  27. if (placeHolder.equals(consonants.substring(j, j + 1))) //Compares the two letters
  28. {
  29. stringArray[i] = userInput.substring(i, i + 1);
  30. intArray[i]++;
  31. }
  32. }
  33. }
  34. for(int i = 0; i < stringArray.length; i++)
  35. result += "\n" + stringArray[i] + "\t" + intArray[i];
  36. }
  37.  
  38.  
  39. else
  40. result = "Invalid input.";
  41.  
  42.  
  43. JOptionPane.showMessageDialog(null, result, "Consonant Content", 2);
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment