Advertisement
Guest User

Untitled

a guest
Feb 8th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.03 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. public class Project1
  3. {
  4. public static void main(String [] args)
  5. {
  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: VowelAndConsonant(); break;
  20. case 2: qwerty(); break;
  21. case 3: shortestAndLongest(); break;
  22. case 4: areTheyAnagrams(); break;
  23. case 5: isItAPalindrome(); break;
  24. case 6: analyzeCharacterContent(); break;
  25. }
  26. }
  27. }
  28. }
  29. }
  30.  
  31. public static String getMenuOption()
  32. {
  33. String menuOptions = "1. Analyse vowel/consonant content of word/phrase.\n" +
  34. "2. What keyboard rows were used?\n" +
  35. "3. Display the shortest and longest words in a sentence.\n" +
  36. "4. Are two words/phrases anagrams of each other?\n" +
  37. "5. Is the word/phrase a palindrome?\n" +
  38. "6. Analyse character content of word/phrase.\n" +
  39. "0. Exit.\n";
  40. String menuMessage = "Choose number of option you wish to have executed";
  41. String errorMessage = "Invalid menu selection.\n\nValid options are 0 to 7 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. public static void VowelAndConsonant()
  58. {
  59. String message1 = "Choose an option:\n"+
  60. "1. Analyze vowel and consonant content\n"+
  61. "2. Check for alternating vowels and consonants";
  62. String message2 = "Analyze word";
  63. String pattern = "[1-2]";
  64. int input = Integer.parseInt(getWordOrPhraseFromEndUser(message1, message2, pattern));
  65.  
  66. switch(input)
  67. {
  68. case 1: analyzeVowelConsonantContentOfWordPhrase(); break;
  69. case 2: alternatingVowelsConsonants(); break;
  70. }
  71. }
  72.  
  73.  
  74. public static void analyzeVowelConsonantContentOfWordPhrase()
  75. {
  76. String message2 = "Analyze vowel and consonant contents";
  77. String message1 = "Enter a word/phrase";
  78. String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  79. String input = getWordOrPhraseFromEndUser(message1, message2, pattern);
  80.  
  81. String result = "";
  82. String userInput = input.toLowerCase();
  83. char[] testInput = userInput.toCharArray();
  84. String[] test = new String[testInput.length];
  85. for(int j = 0 ; j < testInput.length ; j++)
  86. {
  87. char holder = testInput[j];
  88. test[j] = Character.toString(holder);
  89. }
  90. String vowelPattern = "a|e|i|o|u";
  91. String aPattern = "a";
  92. String ePattern = "e";
  93. String iPattern = "i";
  94. String oPattern = "o";
  95. String uPattern = "u";
  96. boolean onlyConsonants = true;
  97. boolean allVowels = false;
  98. boolean vowelsOrder = false;
  99. boolean vowelsBackwards = false;
  100. int countA = 0;
  101. int countE = 0;
  102. int countI = 0;
  103. int countO = 0;
  104. int countU = 0;
  105. int indexA;
  106. int indexE;
  107. int indexI;
  108. int indexO;
  109. int indexU;
  110.  
  111.  
  112. String consonants = "bcdfghjklmnpqrstvwxyz";
  113. int[] count = new int[21];
  114. String[] conOutput = {"B","C","D","F","G","H","J","K","L","M","N","P","Q","R","S","T","V","W","X","Y","Z"};
  115. String aChar;
  116. int position;
  117. String tempInput = userInput.replaceAll("[^a-zA-Z]","");
  118. String r = "\nConsonant count:";
  119.  
  120. for(int i = 0 ; i < tempInput.length() ; i++)
  121. {
  122. aChar = tempInput.substring(i, i+1);
  123. position = consonants.indexOf(aChar);
  124. if(position != -1)
  125. count[position]++;
  126. }
  127.  
  128. for(int j = 0 ; j < consonants.length() ; j++)
  129. {
  130. if(count[j] != 0)
  131. r += "\n" + conOutput[j] + " occurs " + count[j] + " time( s)";
  132. }
  133.  
  134.  
  135.  
  136.  
  137. for(int j = 0 ; j < testInput.length ; j++)
  138. {
  139. if(test[j].matches(vowelPattern))
  140. onlyConsonants = false;
  141. }
  142. for(int j = 0 ; j < testInput.length ; j++)
  143. {
  144. if(test[j].matches(aPattern))
  145. countA++;
  146. else if(test[j].matches(ePattern))
  147. countE++;
  148. else if(test[j].matches(iPattern))
  149. countI++;
  150. else if(test[j].matches(oPattern))
  151. countO++;
  152. else if(test[j].matches(uPattern))
  153. countU++;
  154. }
  155. if(countA >= 1 && countE >= 1 && countI >= 1 && countO >= 1 && countU >= 1)
  156. {
  157. allVowels = true;
  158. indexA = input.indexOf('a');
  159. indexE = input.indexOf('e');
  160. indexI = input.indexOf('i');
  161. indexO = input.indexOf('o');
  162. indexU = input.indexOf('u');
  163. if(((indexA < indexE)&&(indexE < indexI)&&(indexI < indexO)&&(indexO < indexU)) && ((countA == 1)&&(countE == 1)&&(countI == 1)&&(countO == 1)&&(countU == 1)))
  164. vowelsOrder = true;
  165. else if(((indexA > indexE)&&(indexE > indexI)&&(indexI > indexO)&&(indexO > indexU)) && ((countA == 1)&&(countE == 1)&&(countI == 1)&&(countO == 1)&&(countU == 1)))
  166. vowelsBackwards = true;
  167. }
  168.  
  169.  
  170. if(onlyConsonants)
  171. result += "Input contains no vowels";
  172. else if(vowelsOrder)
  173. result += "Input contains all vowels in alphabetical order\nVowel count:";
  174. else if(vowelsBackwards)
  175. result += "Input contains all vowels in reverse alphabetical order\nVowel count:";
  176. else if(allVowels)
  177. result += "Input contains all vowels in any order\nVowel count:";
  178. else
  179. result += "Input contains vowels and consonants but not all vowels\nVowel count:";
  180.  
  181. if(countA >= 1)
  182. result += "\n A occurs " + countA + " time(s)";
  183. if(countE >= 1)
  184. result += "\n E occurs " + countE + " time(s)";
  185. if(countI >= 1)
  186. result += "\n I occurs " + countI + " time(s)";
  187. if(countO >= 1)
  188. result += "\n O occurs " + countO + " time(s)";
  189. if(countU >= 1)
  190. result += "\n U occurs " + countU + " time(s)";
  191.  
  192. result += r;
  193.  
  194. JOptionPane.showMessageDialog(null, result);
  195.  
  196. }
  197.  
  198. public static void alternatingVowelsConsonants()
  199. {
  200. String message1 = "Are there alternating vowels and consonants?";
  201. String message2 = "Enter word/phrase";
  202. String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  203. String input = getWordOrPhraseFromEndUser(message2, message1, pattern);
  204.  
  205. String result = "";
  206. String vowels = "aeiou";
  207. String consonants = "bcdfghjklmnpqrstvwxyz";
  208. String tempInput = input.toLowerCase();
  209. tempInput = tempInput.replaceAll("[^a-z]","");
  210. String first = tempInput.substring(0,1);
  211. String[] inputArray = tempInput.split("");
  212. boolean conTrue = true;
  213. boolean vowTrue = true;
  214. int i, j;
  215. if(vowels.indexOf(first) != -1)
  216. {
  217. i = 1;
  218. j = 2;
  219. }
  220. else
  221. {
  222. i = 0;
  223. j = 1;
  224. }
  225. while(conTrue && i < inputArray.length)
  226. {
  227. if(consonants.indexOf(inputArray[i]) == -1)
  228. conTrue = false;
  229. i +=2;
  230. }
  231.  
  232. if(conTrue)
  233. {
  234. while(vowTrue && j < inputArray.length)
  235. {
  236. if(vowels.indexOf(inputArray[j]) == -1)
  237. vowTrue = false;
  238. j += 2;
  239. }
  240.  
  241.  
  242.  
  243. }
  244. if(vowTrue && conTrue)
  245. result += "Word/phrase entirely comprises of alternating vowels and consonants";
  246. else
  247. result += "Word/phrase does not entirely comprise of alternating vowels and consonants";
  248.  
  249. JOptionPane.showMessageDialog(null, result);
  250. }
  251.  
  252. public static void qwerty()
  253. {
  254. //***
  255. String message1 = "What keyboard rows are used?";
  256. String message2 = "Enter word/phrase";
  257. String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  258. String input = getWordOrPhraseFromEndUser(message2, message1, pattern);
  259.  
  260. String row1 = "qwertyuiop";
  261. String row2 = "asdfghjkl";
  262. String row3 = "zxcvbnm";
  263. String returnme = input + " can be written using the following rows of a keyboard: \n";
  264.  
  265. int rowcounter = 1;
  266.  
  267. int numbers = 0; int letters = 0; int symbols = 0;
  268. String linput = input.toLowerCase();
  269.  
  270. for(int i = 0; i < linput.length(); i++)
  271. {
  272. if(row1.indexOf(linput.substring(i, i+1)) != -1)
  273. rowcounter *= 3;
  274. else if(row2.indexOf(linput.substring(i, i+1)) != -1)
  275. rowcounter *= 5;
  276. else if(row3.indexOf(linput.substring(i, i+1)) != -1)
  277. rowcounter *= 7;
  278. }
  279. if(rowcounter%3 == 0)
  280. returnme += "Row 1 \n";
  281. if(rowcounter%5 == 0)
  282. returnme += "Row 2 \n";
  283. if(rowcounter%7 == 0)
  284. returnme += "Row 3 \n";
  285.  
  286. JOptionPane.showMessageDialog(null, returnme, "Result", 3);
  287. }
  288.  
  289. public static void shortestAndLongest()
  290. {
  291. String pattern = "([a-zA-z0-9,]+)|((([a-zA-z0-9,]+\\s)+)[a-zA-z0-9,]+)";
  292. String message1 = "Enter word/sentence/phrase";
  293. String message2 = "Which words are longest and shortest";
  294. String userInput = getWordOrPhraseFromEndUser(message1, message2, pattern);
  295.  
  296. //boolean validInput = false;
  297. //String userInput = "";
  298. //String errorMessage = "Invalid Input";
  299. String[] words;
  300. String results = "";
  301. String longest = " ";
  302. String shortest = " ";
  303. int l = 0;
  304. int s = 0;
  305. /*while (!(validInput))
  306. {
  307. userInput = JOptionPane.showInputDialog(null, "Enter a word or sentence or phrase", "Which words are the longest and shortest", 3);
  308. userInput = userInput.replaceAll("\\s+"," ");
  309. userInput = userInput.trim();
  310. if (userInput == null || userInput.matches(pattern))
  311. validInput = true;
  312. else
  313. JOptionPane.showMessageDialog(null, errorMessage, "Error in user input", 2);
  314. }*/
  315. userInput = userInput.replaceAll(",","");
  316. words = userInput.split(" ");
  317. shortest = words[0];
  318. s = shortest.length();
  319. for (int i = 0; i < words.length; i++)
  320. {
  321. if (words[i].length()>longest.length())
  322. {
  323. longest = words[i];
  324. l = longest.length();
  325. }
  326. else if ((words[i].length()==longest.length()) && !(words[i].equalsIgnoreCase(longest)))
  327. longest = words[i] + " + " + longest;
  328. }
  329. for (int i = 1; i < words.length; i++)
  330. {
  331. if (words[i].length()<shortest.length())
  332. {
  333. shortest = words[i];
  334. s = shortest.length();
  335. }
  336. else if ((words[i].length()==shortest.length()) && !(words[i].equalsIgnoreCase(shortest)))
  337. shortest = words[i] + " + " + shortest;
  338. }
  339. results = "The maximum length is " + l + " and the word(s) matching this length are\\is \"" + longest + "\"\n";
  340. results += "The minimum length is " + s + " and the word(s) matching this length are\\is \"" + shortest + "\"";
  341. JOptionPane.showMessageDialog(null, results, "Results", 3);
  342.  
  343. //System.out.print(longest + " " + shortest);
  344.  
  345. }
  346.  
  347. public static void areTheyAnagrams()
  348. {
  349. String phrase1, phrase2, results;
  350. String c;
  351. int j = 0;
  352. //char[] p1 = new char[phrase1.length()];
  353. //char[] p2 = new char[phrase2.length()];
  354. String message1 = "Are words/phrases anagrams?";
  355. String message2 = "Enter first word/phrase";
  356. String message3 = "Enter second word/phrase";
  357. String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  358. phrase1 = getWordOrPhraseFromEndUser(message2, message1, pattern);
  359. if (phrase1 != null)
  360. {
  361. phrase2 = getWordOrPhraseFromEndUser(message3, message1, pattern);
  362. if (phrase2 != null)
  363. {
  364. phrase1 = phrase1.replaceAll(" ","");
  365. phrase2 = phrase2.replaceAll(" ","");
  366. phrase1 = phrase1.trim();
  367. phrase2 = phrase2.trim();
  368. if (phrase1.length() != phrase2.length())
  369. results = "They are not anagrams";
  370. else
  371. {
  372. while(j<phrase2.length())
  373. {
  374. c = phrase1.substring(0,1);
  375. if (c.equals(phrase2.substring(j,j+1)))
  376. {
  377. if (phrase2.length() != 1)
  378. {
  379. phrase1 = phrase1.substring(1);
  380. if (j != 0)
  381. phrase2 = phrase2.substring(0,j) + phrase2.substring(j+1,phrase2.length());
  382. else
  383. phrase2 = phrase2.substring(j+1,phrase2.length());
  384. j=0;
  385. }
  386. else
  387. {
  388. phrase2 = "";
  389. phrase1 = "";
  390. }
  391. }
  392. else
  393. j++;
  394. }
  395. }
  396. if (phrase1.length()==0)
  397. JOptionPane.showMessageDialog(null, "They are anagrams", "Result", 3);
  398. else
  399. JOptionPane.showMessageDialog(null, "They are not anagrams", "Result", 3);
  400. }
  401. }
  402. }
  403.  
  404. public static void isItAPalindrome()
  405. {
  406. String input, temp, result = "";
  407. boolean palindrome1 = false, palindrome2 = false;
  408. String[] myarray;
  409.  
  410. String message1 = "Is the word/phrase a palindrome?";
  411. String message2 = "Enter word/phrase";
  412. String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  413. input = getWordOrPhraseFromEndUser(message2, message1, pattern);
  414. input = input.toLowerCase();
  415.  
  416. if (input != null)
  417. {
  418. if(input.indexOf(" ") == -1) //(WORD)
  419. {
  420. //Check for regular palindrome (WORD)
  421. myarray = input.split("");
  422. int i = 0, j = myarray.length - 1;
  423. do
  424. {
  425. //if(myarray[i].compareTo(myarray[j]) == 0)
  426. if(myarray[i].equals(myarray[j]))
  427. palindrome1 = true;
  428. else{
  429. palindrome1 = false;
  430.  
  431. }
  432. i++; j--;
  433. }while( i < myarray.length && palindrome1);
  434. }
  435. else //(PHRASE)
  436. {
  437. //Check for word-unit palindrome (PHRASE)
  438. myarray = input.split("\\s");
  439. int i = 0, j = myarray.length - 1;
  440. do
  441. {
  442. if(myarray[i].equals(myarray[j]))
  443. palindrome2 = true;
  444. else
  445. palindrome2 = false;
  446.  
  447. i++; j--;
  448. }
  449. while( i < myarray.length && palindrome2);
  450.  
  451. //CHeck for regular palindrome(PHRASE)
  452. temp = input.replaceAll("[^a-z]","");
  453. myarray = temp.split("");
  454. i = 0; j = myarray.length - 1;
  455. do
  456. {
  457. if(myarray[i].equals(myarray[j]))
  458. palindrome1 = true;
  459. else
  460. palindrome1 = false;
  461.  
  462. i++; j--;
  463. }
  464. while( i < myarray.length && palindrome1);
  465.  
  466. }
  467. String firstLetter = input.substring(0,1).toUpperCase();
  468. input = firstLetter + input.substring(1);
  469.  
  470. if(palindrome1 && palindrome2)
  471. result = input + " is a regular palindrome and also a palindrome at the word-unit level.";
  472. else if(palindrome1)
  473. result = input + " is a regular palindrome";
  474. else if(palindrome2)
  475. result = input + " is a palindrome at the word-unit level";
  476. else
  477. result = input + " is not a palindrome";
  478.  
  479. JOptionPane.showMessageDialog(null, result, "Result", 3);
  480. }
  481. }
  482.  
  483. public static void analyzeCharacterContent()
  484. {
  485. //***
  486. String message1 = "Analyze character content";
  487. String message2 = "Enter word/phrase";
  488. String input = JOptionPane.showInputDialog(null, message2, message1, 3);
  489. //***
  490.  
  491. //we don't need to differentiate between upper and lower case
  492. String lowerinput = input.toLowerCase();
  493. String result = "";
  494. //128 is not a valid symbol
  495. int[] ascii = new int[127];
  496. //takes the int value of the char at i and increments index i of ascii array
  497. for(int i=0; i < input.length(); i++)
  498. {
  499. int holder = (int) lowerinput.charAt(i);
  500. ascii[holder] = ascii[holder] + 1;
  501. }
  502. //checks ascii i isnt empty, then concats the char associated with ascii i
  503. //and the value at ascii i to result with some fluff.
  504. for(int i=33; i<127; i++)
  505. {
  506. if(ascii[i] != 0)
  507. {
  508. char concat = (char) i;
  509. int counter = ascii[i];
  510. result += concat + " appears " + counter + " times \n";
  511. }
  512. }
  513. //***
  514. JOptionPane.showMessageDialog(null, result, "Result", 3);
  515. }
  516.  
  517. public static String getWordOrPhraseFromEndUser(String windowMessage, String windowTitle, String pattern)
  518. {
  519. boolean validInput = false;
  520. String userInput = "";
  521. //String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  522. String errorMessage = "Invalid input.";
  523. errorMessage += "\n\nEnter a word or phrase comprising ";
  524. errorMessage += "alphabetic characters only.";
  525. errorMessage += "\nSelect OK to retry.";
  526.  
  527. while (!(validInput))
  528. {
  529. userInput = JOptionPane.showInputDialog(null, windowMessage, windowTitle, 3);
  530. if (userInput == null || userInput.matches(pattern))
  531. validInput = true;
  532. else
  533. JOptionPane.showMessageDialog(null, errorMessage, "Error in user input", 2);
  534. }
  535. return userInput;
  536. }
  537. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement