Advertisement
Guest User

Untitled

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