Advertisement
Guest User

p1

a guest
Feb 7th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.14 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Analyser
  4. {
  5. public static void main(String [] args)
  6. {
  7. int choice;
  8. String menuOption = "";
  9. while ((menuOption != null) && (!(menuOption.equals("0"))))
  10. {
  11. menuOption = getMenuOption();
  12. if (menuOption != null)
  13. {
  14. choice = Integer.parseInt(menuOption);
  15. if (choice != 0)
  16. {
  17. switch(choice)
  18. {
  19. case 1: analyzeVowelContentOfWordPhrase(); break;
  20. case 4: keyboardRows(); break;
  21. case 5: alternatingVowelsAndConsonants(); break;
  22. case 6: determineLongestAndShortestWords(); break;
  23. }
  24. }
  25. }
  26. }
  27. }
  28.  
  29. public static String getMenuOption()
  30. {
  31. String menuOptions = "1. Analyze vowel content of word/phrase." +
  32. "\n2. Analyze consonant content of word/phrase" +
  33. "\n3. Analyze character content of word/phrase" +
  34. "\n4. Determine which row(s) of keys on QWERTY keyboard need to be used to type word/phrase" +
  35. "\n5. Analyze vowel and consonant content of word/phrase" +
  36. "\n6. Length of shortest/longest word and matches of each." +
  37. "\n7. Are two words/phrases anagrams of each other ?" +
  38. "\n8. Is word/phrase a palindrome ?" +
  39. "\n0. Exit.";
  40. String menuMessage = "Choose number of option you wish to have executed";
  41. String errorMessage = "Invalid menu selection.\n\nValid options are 0 to 8 inclusive.";
  42. errorMessage += "\nSelect OK to retry.";
  43. String errorHeader = "Error in user input";
  44. boolean validInput = false;
  45. String selection = "", menuChoicePattern = "[0-7]{1}";
  46.  
  47. while (!(validInput))
  48. {
  49. selection = JOptionPane.showInputDialog(null, menuOptions, menuMessage, 3);
  50. if (selection == null || selection.matches(menuChoicePattern))
  51. validInput = true;
  52. else
  53. JOptionPane.showMessageDialog(null, errorMessage, errorHeader, 2);
  54. }
  55. return selection;
  56. }
  57.  
  58. // John Long-12132306
  59. public static void analyzeVowelContentOfWordPhrase()
  60. {
  61. String msg1 = "Vowel content analyzer";
  62. String msg2 = "Enter word/phrase";
  63. String vowels = "aeiou";
  64. String input = getWordOrPhraseFromEndUser(msg1,msg2);
  65. String tempInput = input.toLowerCase();
  66. tempInput = tempInput.trim(); // for leading or trailing spaces
  67. tempInput = tempInput.replaceAll(" ","");
  68.  
  69. boolean allPresent = false;
  70. String report = "";
  71. String aVowel;
  72. int vCount [] = {0,0,0,0,0};
  73. char vChar;
  74. String vowelPtrn = "^[aeiou]";
  75.  
  76. // check for any vowels
  77. if(tempInput.matches(vowelPtrn))
  78. report += "Input contains no vowels\n";
  79.  
  80. // check for all vowels presence
  81. for(int i=0; i<vowels.length(); i++)
  82. {
  83. aVowel = vowels.substring(i,i+1);
  84. if(tempInput.indexOf(aVowel) == -1)
  85. allPresent = false;
  86. else
  87. allPresent = true;
  88. }
  89.  
  90.  
  91.  
  92. // *** count vowels for which there is at least 1 occurrence
  93. for(int i=0; i<vowels.length(); i++)
  94. {
  95. vChar = vowels.charAt(i);
  96. for(int j=0; j<tempInput.length(); j++)
  97. {
  98. if(vChar == tempInput.charAt(j))
  99. vCount[i]++;
  100. }
  101. }
  102.  
  103.  
  104. //report += "Vowel frequencies are\n"+"a="+vCount[0]+"\n"+"e="+vCount[1]+"\n"+"i="+vCount[2]+"\n"+"o="+vCount[3]+"\n"+"u="+vCount[4];
  105.  
  106. // check for alphabetic normal+reverse order
  107. if(allPresent)
  108. {
  109. report += "All vowels are present\n";
  110. }else
  111. report += "All vowels not present\n";
  112.  
  113. JOptionPane.showMessageDialog(null,report,"Report",1);
  114. }
  115.  
  116.  
  117. // John Long-12132306
  118. public static void determineLongestAndShortestWords()
  119. {
  120. String msg1 = "Word length/match analyzer";
  121. String msg2 = "Enter word/phrase/sentence";
  122. String input = getWordOrPhraseFromEndUser(msg1,msg2);
  123. String tempInput = input.toLowerCase();
  124. tempInput = tempInput.trim(); // for leading or trailing spaces
  125. String words [] = tempInput.split(" ");
  126. String report = "";
  127. String longestMatches = "";
  128. String shortestMatches = "";
  129.  
  130. // assume 1st word is longest and shortest
  131. // longword/shortword are placeholders for the longest/shortest words
  132. String longWord = words[0];
  133. String shortWord = words[0];
  134.  
  135. int longest = longWord.length();
  136. int shortest = shortWord.length();
  137.  
  138. // iterate to find longest and shortest sizes first
  139. for(int i=0; i<words.length; i++)
  140. {
  141. // compare and update longest/shortest word
  142. if(words[i].length() > longest)
  143. longWord = words[i];
  144. else if(words[i].length() < shortest)
  145. shortWord = words[i];
  146. }
  147.  
  148. // iterate to find matches for shortest/longest sizes in input
  149. for(int i=0; i<words.length; i++)
  150. {
  151. // if word matches longest/shortest then populate appropriate array
  152. if(words[i].length() == longest)
  153. longestMatches += words[i] + " ";
  154.  
  155. else if(words[i].length() == shortest)
  156. shortestMatches += words[i] + " ";
  157. }
  158.  
  159. report += "Max length = " + longest + "\n" + "Matching words are\n" + longestMatches + "\n";
  160. report += "Min length = " + shortest + "\n" + "Matching words are\n" + shortestMatches + "\n";
  161.  
  162. JOptionPane.showMessageDialog(null,report,"Report",1);
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169. public static void alternatingVowelsAndConsonants()
  170. {
  171. //Jamie McLoughlin
  172. String tempInput = "";
  173. String windowMessagew = "Please enter words/phrases \n with alphabetic characters only.";
  174. String windowTitle = "Alternating Vowels and Consonants.";
  175.  
  176. //Get word from end used and cheak to see if it only Alphabetic Characters Only.
  177. String userInput = getWordOrPhraseFromEndUser(windowMessagew,windowTitle);
  178. String message1 = userInput + " does have alternating Vowels and Consonants.";
  179. String message2 = userInput + " does not have alternating Vowels and Consonants.";
  180.  
  181.  
  182. //Get rid of any white spaces and turn any uppercase to lowercase
  183. tempInput = userInput.replaceAll("[\\s]","");
  184. tempInput = tempInput.toLowerCase();
  185.  
  186. /*The pattern starts with a Consonants followed by a Vowels and alternating until it either ends with or without a Consonants
  187. or the opposite starting with a Vowel.*/
  188. String pattern = "(([^aeiou][aeiou])+[^aeiou]?)|(([aeiou][^aeiou])+[aeiou]?)";
  189.  
  190. //Just checked to see if the word matches the pattern and Returns a message if it does or not.
  191. if (tempInput.matches(pattern))
  192. {
  193. JOptionPane.showMessageDialog(null, message1, windowTitle, 1);
  194. }
  195. else
  196. {
  197. JOptionPane.showMessageDialog(null, message2, windowTitle, 1);
  198. }
  199. }
  200.  
  201. public static void keyboardRows()
  202. {
  203. //Jamie McLoughlin
  204. String windowMessagew = "Please enter words/phrases \n with alphabetic characters only.";
  205. String windowTitle = "Keyboard rows.";
  206. String tempInput = "";
  207. String message ="You need to use rows";
  208. String message2 = " 1.", message3 = " 2.", message4 = " 3.";
  209. String row1 = "[qwertyuiop]", row2 = "[asdfghjkl]";
  210. int i = 0;
  211. boolean useRow1 = false,useRow2 = false,useRow3 = false,useAllRows = false;
  212.  
  213. //Get word from end used and cheak to see if it only Alphabetic Characters Only.
  214. String userInput = getWordOrPhraseFromEndUser(windowMessagew,windowTitle);
  215.  
  216. //Get rid of any white spaces and turn any uppercase to lowercase
  217. tempInput = userInput.replaceAll("[\\s]","");
  218. tempInput = tempInput.toLowerCase();
  219.  
  220. /*Go through the while loop and if the Alphabetic Characters matches the Regular Expression
  221. for that row it turns the boolean to true. If all rows are true it stops looking.
  222. Then shows the user the rows needed to use*/
  223. while (i < tempInput.length() && !useAllRows)
  224. {
  225. if ((tempInput.substring(i,i+1)).matches(row1))
  226. {
  227. if (!useRow1){
  228. message += message2;
  229. }useRow1 = true;
  230. }
  231. else if ((tempInput.substring(i,i+1)).matches(row2))
  232. {
  233. if (!useRow2){
  234. message += message3;
  235. }useRow2 = true;
  236. }
  237. else
  238. {
  239. if (!useRow3){
  240. message += message4;
  241. }useRow3 = true;
  242. }
  243. if (useRow1 && useRow2 && useRow3)
  244. {
  245. useAllRows = true;
  246. }
  247. i++;
  248. }
  249. message += " to tpye " + userInput;
  250. JOptionPane.showMessageDialog(null, message, windowTitle, 1);
  251. }
  252.  
  253.  
  254. public static String getWordOrPhraseFromEndUser(String windowMessage, String windowTitle)
  255. {
  256. boolean validInput = false;
  257. String userInput = "";
  258. String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  259. String errorMessage = "Invalid input.";
  260. errorMessage += "\n\nEnter a word or phrase comprising ";
  261. errorMessage += "alphabetic characters only.";
  262. errorMessage += "\nSelect OK to retry.";
  263.  
  264. while (!(validInput))
  265. {
  266. userInput = JOptionPane.showInputDialog(null, windowMessage, windowTitle, 3);
  267. if (userInput == null || userInput.matches(pattern))
  268. validInput = true;
  269. else
  270. JOptionPane.showMessageDialog(null, errorMessage, "Error in user input", 2);
  271. }
  272. return userInput;
  273. }
  274.  
  275. public static String getWordOrPhraseFromEndUserLtrsandNums(String windowMessage, String windowTitle)
  276. {
  277. boolean validInput = false;
  278. String userInput = "";
  279. String pattern = "(([a-zA-Z0-9])+)|(((([a-zA-Z0-9])+\\s)+)([a-zA-Z0-9])+)";
  280. String errorMessage = "Invalid input.";
  281. errorMessage += "\n\nEnter a word or phrase comprising ";
  282. errorMessage += "alphabetic and numeric characters only.";
  283. errorMessage += "\nSelect OK to retry.";
  284.  
  285. while (!(validInput))
  286. {
  287. userInput = JOptionPane.showInputDialog(null, windowMessage, windowTitle, 3);
  288. if (userInput == null || userInput.matches(pattern))
  289. validInput = true;
  290. else
  291. JOptionPane.showMessageDialog(null, errorMessage, "Error in user input", 2);
  292. }
  293. return userInput;
  294. }
  295.  
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement