Advertisement
Guest User

Untitled

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