Advertisement
ruthiek

Untitled

Feb 6th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. public static void analyzeVowelConsonantContentOfWordPhrase(String Input)
  2. {
  3. String result = "";
  4. String userInput = Input.toLowerCase();
  5. char[] testInput = userInput.toCharArray();
  6. String[] test = new String[testInput.length];
  7. for(int j = 0 ; j < testInput.length ; j++)
  8. {
  9. char holder = testInput[j];
  10. test[j] = Character.toString(holder);
  11. }
  12. String vowelPattern = "a|e|i|o|u";
  13. String aPattern = "a";
  14. String ePattern = "e";
  15. String iPattern = "i";
  16. String oPattern = "o";
  17. String uPattern = "u";
  18. boolean onlyConsonants = true;
  19. boolean allVowels = false;
  20. boolean vowelsOrder = false;
  21. boolean vowelsBackwards = false;
  22. int countA = 0;
  23. int countE = 0;
  24. int countI = 0;
  25. int countO = 0;
  26. int countU = 0;
  27. int indexA;
  28. int indexE;
  29. int indexI;
  30. int indexO;
  31. int indexU;
  32.  
  33.  
  34. String consonants = "bcdfghjklmnpqrstvwxyz";
  35. int[] count = new int[21];
  36. String[] conOutput = {"B","C","D","F","G","H","J","K","L","M","N","P","Q","R","S","T","V","W","X","Y","Z"};
  37. String aChar;
  38. int position;
  39. String tempInput = userInput.replaceAll("[^a-zA-Z]","");
  40. String r = "\nConsonant count:";
  41.  
  42. for(int i = 0 ; i < tempInput.length() ; i++)
  43. {
  44. aChar = tempInput.substring(i, i+1);
  45. position = consonants.indexOf(aChar);
  46. if(position != -1)
  47. count[position]++;
  48. }
  49.  
  50. for(int j = 0 ; j < consonants.length() ; j++)
  51. {
  52. if(count[j] != 0)
  53. r += "\n" + conOutput[j] + " occurs " + count[j] + " time( s)";
  54. }
  55.  
  56.  
  57.  
  58.  
  59. for(int j = 0 ; j < testInput.length ; j++)
  60. {
  61. if(test[j].matches(vowelPattern))
  62. onlyConsonants = false;
  63. }
  64. for(int j = 0 ; j < testInput.length ; j++)
  65. {
  66. if(test[j].matches(aPattern))
  67. countA++;
  68. else if(test[j].matches(ePattern))
  69. countE++;
  70. else if(test[j].matches(iPattern))
  71. countI++;
  72. else if(test[j].matches(oPattern))
  73. countO++;
  74. else if(test[j].matches(uPattern))
  75. countU++;
  76. }
  77. if(countA >= 1 && countE >= 1 && countI >= 1 && countO >= 1 && countU >= 1)
  78. {
  79. allVowels = true;
  80. indexA = Input.indexOf('a');
  81. indexE = Input.indexOf('e');
  82. indexI = Input.indexOf('i');
  83. indexO = Input.indexOf('o');
  84. indexU = Input.indexOf('u');
  85. if(((indexA < indexE)&&(indexE < indexI)&&(indexI < indexO)&&(indexO < indexU)) && ((countA == 1)&&(countE == 1)&&(countI == 1)&&(countO == 1)&&(countU == 1)))
  86. vowelsOrder = true;
  87. else if(((indexA > indexE)&&(indexE > indexI)&&(indexI > indexO)&&(indexO > indexU)) && ((countA == 1)&&(countE == 1)&&(countI == 1)&&(countO == 1)&&(countU == 1)))
  88. vowelsBackwards = true;
  89. }
  90.  
  91.  
  92. if(onlyConsonants)
  93. result += "Input contains no vowels";
  94. else if(vowelsOrder)
  95. result += "Input contains all vowels in alphabetical order\nVowel count:";
  96. else if(vowelsBackwards)
  97. result += "Input contains all vowels in reverse alphabetical order\nVowel count:";
  98. else if(allVowels)
  99. result += "Input contains all vowels in any order\nVowel count:";
  100. else
  101. result += "Input contains vowels and consonants but not all vowels\nVowel count:";
  102.  
  103. if(countA >= 1)
  104. result += "\n A occurs " + countA + " time(s)";
  105. if(countE >= 1)
  106. result += "\n E occurs " + countE + " time(s)";
  107. if(countI >= 1)
  108. result += "\n I occurs " + countI + " time(s)";
  109. if(countO >= 1)
  110. result += "\n O occurs " + countO + " time(s)";
  111. if(countU >= 1)
  112. result += "\n U occurs " + countU + " time(s)";
  113.  
  114. result += r;
  115.  
  116. JOptionPane.showMessageDialog(null, result);
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement