Advertisement
Guest User

Untitled

a guest
May 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.78 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. import java.util.Arrays;
  3. public class UserValidation
  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: analyseVowelContentOfWordPhrase(); break;
  20. case 2: analyseConsonantContent(); break;
  21. case 3: analyseCharacterContent(); break;
  22. case 4: determineKeyboardRows(); break;
  23. case 5: alternatingVowelsAndConsonants(); break;
  24. case 6: determineLongestAndShortestWords(); break;
  25. case 7: areWordsOrPhrasesAnagrams(); break;
  26. case 8: determineIfPalindromes(); break;
  27. }
  28. }
  29. }
  30. }
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. public static String getMenuOption()
  38. {
  39. String menuOptions = "1. Analyse vowel content of word/phrase."+"\n2. Analyse consonant content of word/phrase."+"\n3. Analyse character content of word/phrase" +"\n4. Determine keyboard rows required for word/phrase"+"\n5. Determine if vowels and consonants are alternating in word/phrase."+"\n6. Display the shortest and longest words in a sentence."+"\n7. Are two words/phrases anagrams of each other?"+"\n8. Determine if word/phrase is palindrome."+"\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 7 inclusive.";
  42. errorMessage += "\nSelect OK to retry.";
  43. String errorHeader = "Error in user input";
  44. boolean validInput = false;
  45. String selection = "", menuChoicePattern = "[0-8]{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.  
  59.  
  60.  
  61.  
  62. public static void analyseVowelContentOfWordPhrase()
  63. {
  64. String input = "", msg1, msg2, msg3, msg4, msg5, msg6, result = "",error;
  65. error = "Invalid input";
  66. int a ,e, i , o , u, x=0;
  67. char letter;
  68. msg1 = "Please enter a word or sentence:";
  69. while(input != null)
  70. {
  71. a = 0; e = 0; i = 0; o = 0; u = 0;
  72. result = "";
  73. input = JOptionPane.showInputDialog(null, msg1, "Find vowel content", 3);
  74.  
  75. if(input == null)
  76. {return;}
  77. else
  78. {
  79. input = input.toLowerCase();
  80. int posA,posE,posI,posO,posU;
  81. posA = input.indexOf('a');
  82. posE = input.indexOf('e');
  83. posI = input.indexOf('i');
  84. posO = input.indexOf('o');
  85. posU = input.indexOf('u');
  86. msg2 = "There are no vowels present";
  87. msg3 = "This input contains all the vowels";
  88. msg4 = "This input contains all the vowels in alphabetical order";
  89. msg5 = "This input contains all the vowels in reverse alphabetical order";
  90. if(posA == -1 && posE == -1 && posI == -1 && posO == -1 && posU == -1) //If no vowels present
  91. {
  92. result += msg2;
  93. }
  94. else
  95. {
  96. if(posA != -1 && posE != -1 && posI != -1 && posO != -1 && posU != -1) //If all vowels present
  97. {
  98. if(posA < posE && posE < posI && posI < posO && posO < posU) //If all present in alphabetical order
  99. {
  100. result += msg4;
  101. }
  102. else if(posA > posE && posE > posI && posI > posO && posO > posU) //If all present in reverse alphabetical order
  103. {
  104. result += msg5;
  105.  
  106. }
  107. else
  108. result += msg3;
  109. }
  110. for(x = 0; x< input.length(); x++)
  111. {
  112. letter = input.charAt(x);
  113. if(letter=='a')
  114. a++;
  115. else if(letter=='e')
  116. e++;
  117. else if(letter=='i')
  118. i++;
  119. else if(letter=='o')
  120. o++;
  121. else if(letter=='u')
  122. u++;
  123. }
  124. if(a > 0)
  125. result += "\n The frequency of 'a' is: " + a;
  126. if(e > 0)
  127. result += "\n The frequency of 'e' is: " + e;
  128. if(i > 0)
  129. result += "\n The frequency of 'i' is: " + i;
  130. if(o > 0)
  131. result += "\n The frequency of 'o' is: " + o;
  132. if(u > 0)
  133. result += "\n The frequency of 'u' is: " + u;
  134. }
  135. JOptionPane.showMessageDialog(null, result, "Results", 1);
  136. }
  137.  
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. public static void analyseConsonantContent()
  147. {
  148. boolean validInput = false;
  149. String result = "";
  150. while(!validInput)
  151. {
  152. String errorMsg = "Please enter a valid word/phrase,\nContaining only letters of the English alphabet";
  153. String userInput = JOptionPane.showInputDialog(null,"Please Enter The Word Or Phrase to check for consonants","Consonant Content",3);
  154. if(userInput==null)return;
  155.  
  156. result+="Original Word/Phrase= "+userInput+"\n"; //prints original string
  157. userInput = userInput.toLowerCase(); //converts to lower case
  158. userInput = userInput.replaceAll("[\\saeiou]", ""); //delete white spaces & vowels
  159. userInput = userInput.replaceAll("[^a-zA-Z]",""); //gets rid of everything else
  160.  
  161. if(userInput.equals(""))
  162. JOptionPane.showMessageDialog(null,errorMsg,"Error",2); //empty string wont be accepted
  163. else
  164. {
  165. validInput=true;
  166. result+="New String (Containing only Consonants)= "+userInput+"\n"; //prints without vowels
  167. char letters [] = userInput.toCharArray(); //puts consonants into char array
  168. Arrays.sort(letters); //sorts the array alphabetically
  169. result+="Total Amount of Consonants= "+letters.length+"\n"; //prints total amount of Consonants
  170.  
  171. int count = 0; //sets consonant counter to 0
  172. char lastConsonant=letters[0]; //initialises consonant position indicator
  173.  
  174. for(int i=0;i<letters.length;i++)
  175. {
  176. if(letters[i]!=lastConsonant) //if it doesnt match next cons, it'll stop the matching process
  177. {
  178. result+=lastConsonant+" occurred "+count+" times\n";
  179. count=0; // prepare for next grouping
  180. lastConsonant=letters[i]; //makes last cons the next char in the sorted array
  181. }
  182. count++; //sets the counter back to one(if a new char exists it must be at least 1)
  183. if(i==(letters.length-1)) //print statement in case the last char is unique
  184. {
  185. result+=lastConsonant+" occurred "+count+" times";
  186. }
  187. }
  188.  
  189. }
  190. }
  191. JOptionPane.showMessageDialog(null,result);
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198. public static void analyseCharacterContent()
  199. {
  200. String userInput, minusAlphabet, minusNumbers, minusSymbols, result1, result2="";
  201. int num1, num2, num3;
  202. int freq=0;
  203. char character;
  204. boolean flag;
  205. do
  206. {
  207. userInput = JOptionPane.showInputDialog(null, "Enter word, phrases, or sentence.","Alphabetic, numeric, and symbolic character analysis", 3);
  208. if (userInput == null) return;
  209. if(userInput.equals(""))
  210. {
  211. JOptionPane.showMessageDialog(null,"Please enter a valid word/sentence/phrase.");
  212. flag=false;
  213. }
  214. else
  215. flag=true;
  216. } while(!flag);
  217. for (character = (char)32; character<=126; character++)
  218. {
  219. freq=0;
  220. for(int i=0; i<userInput.length(); i++)
  221. {
  222. if(character == userInput.charAt(i))
  223. freq++;
  224. }
  225. if(freq>0)
  226. {
  227. result2 += (character+" occurs "+freq+"\n");
  228. }
  229. }
  230. minusAlphabet=userInput.replaceAll("[a-zA-Z]","");
  231. minusNumbers=userInput.replaceAll("[0-9]", "");
  232. minusSymbols=userInput.replaceAll("[^a-zA-Z0-9]","");
  233. num1=userInput.length()-minusAlphabet.length();
  234. num2=userInput.length()-minusNumbers.length();
  235. num3=userInput.length()-minusSymbols.length();
  236. result1="There are "+num1+" alphabetical characters.\nThere are "+num2+" numerical characters.\nThere are "+num3+" symbolic characters.\n";
  237. JOptionPane.showMessageDialog(null, result1+result2, "Results", 3);
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244. public static void determineKeyboardRows()
  245. {
  246. String input, newInput, aCharacter;
  247. String Line1 = "qwertyuiop", Line2 = "asdfghjkl", Line3 = "zxcvbnm", result = "The Keyboard Lines Used In This Word/Sentence/Phrase Are: \n";
  248. boolean L1 = false, L2 = false, L3 = false, isCorrectFormat = false ;
  249. int i;
  250.  
  251. while(isCorrectFormat != true)
  252. {
  253. input = JOptionPane.showInputDialog(null, "Please enter a word, phrase or sentence: ","Keyboard Line Check", 3);
  254. if(input == null)return;
  255.  
  256. newInput = input.replaceAll("[^a-zA-Z]", "");
  257. newInput = newInput.toLowerCase();
  258.  
  259. if(newInput.equals(""))
  260. JOptionPane.showMessageDialog(null,"Please enter a valid word/sentence/phrase."); //Error Message
  261.  
  262. else
  263. {
  264. for(i = 0; i < Line1.length() && !L1; i++)
  265. {
  266. aCharacter = Line1.substring(i, i + 1); //Stores a character from keyboard line 1 into aCharacter
  267. if (input.indexOf(aCharacter) == -1) //Checks for the index of a character in the user input
  268. L1 = false;
  269. else L1 = true;
  270. }
  271.  
  272. for(i = 0; i < Line2.length() && !L2; i++)
  273. {
  274. aCharacter = Line2.substring(i, i + 1);
  275. if (input.indexOf(aCharacter) == -1)
  276. L2 = false;
  277. else L2 = true;
  278. }
  279.  
  280. for(i = 0; i < Line3.length() && !L3; i++)
  281. {
  282. aCharacter = Line3.substring(i, i + 1);
  283. if (input.indexOf(aCharacter) == -1)
  284. L3 = false;
  285. else L3 = true;
  286. }
  287.  
  288. if(L1)
  289. result += "Line 1 (QWERTYUIOP)\n";
  290. if(L2)
  291. result += "Line 2 (ASDFGHJKL) \n";
  292. if(L3)
  293. result += "Line 3 (ZXCVBNM) \n";
  294.  
  295. JOptionPane.showMessageDialog(null,result);
  296. isCorrectFormat = true; //Allows user out of the while loop
  297. }
  298. }
  299. }
  300.  
  301.  
  302.  
  303.  
  304.  
  305. public static void alternatingVowelsAndConsonants()
  306. {
  307. boolean alternating;
  308. String result = "", error;
  309. error = "Invalid input";
  310. String input = "", msg1;
  311. String vowels = "aeiou";
  312. String consonants = "bcdfghjklmnpqrstvwxyz";
  313. boolean validInput;
  314. while(input != null)
  315. {
  316. validInput = false;
  317. alternating = true;
  318. input = "";
  319. result = "";
  320. msg1 = "Please enter a word or sentence";
  321. input = JOptionPane.showInputDialog(null, msg1, "Alternaing vowels and consonants", 3);
  322.  
  323. if(input == null)
  324. {return;}
  325. else
  326. {
  327. input = input.replace(" ","");
  328. String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  329. if(input.matches(pattern))
  330. validInput = true;
  331.  
  332. if((validInput == true) && (input != null))
  333. {
  334. for(int i = 0; i < input.length() -1 && alternating; i++)
  335. {
  336. if(vowels.contains(input.substring(i, i + 1))) //Checks if character is a vowel
  337. {
  338. if(consonants.contains(input.substring(i + 1, i + 2))) //Checks if next character is a consonant
  339. alternating = true;
  340. else alternating = false;
  341. }
  342. else if(consonants.contains(input.substring(i, i + 1))) // Checks if character is a consonant
  343. {
  344. if(vowels.contains(input.substring(i + 1, i + 2))) //Checks if character is a vowel
  345. alternating = true;
  346. else alternating = false;
  347. }
  348. else alternating = false;
  349. }
  350. }
  351. else
  352. {
  353. alternating = false;
  354. }
  355. if(alternating)
  356. result += "This contains alternating vowels and consonants";
  357. else if(!alternating)
  358. result += "This does not contain alternating vowels and consonants";
  359.  
  360. JOptionPane.showMessageDialog(null, result, "Results", 1);
  361.  
  362.  
  363. }
  364.  
  365.  
  366. }
  367. }
  368.  
  369.  
  370.  
  371.  
  372.  
  373. public static void determineLongestAndShortestWords()
  374. {
  375. boolean isCorrectFormat = false, flag =true;
  376. String shortWord = "", longWord = "", input="", words[];
  377. int s=0, l=0;
  378.  
  379. while(isCorrectFormat != true)
  380. {
  381. do
  382. {
  383. input = JOptionPane.showInputDialog(null, "Please enter a word, phrase or sentence:", "Longest Word/Shortest Word", 3);
  384. if(input==null)return;
  385. input = input.replaceAll("[^A-Za-z0-9\\s]","");
  386. s = input.length(); //l = 0; // Variables used for comparisson to find the longest and shortest words
  387. words = input.split(" "); // Splits the user input into an array of strings
  388.  
  389.  
  390. if (input.equals(""))
  391. {
  392. JOptionPane.showMessageDialog(null, "Please input a valid word/sentence/phrase.");
  393. flag=false;
  394. }
  395. else
  396. flag=true;
  397. }while(!flag);
  398. if(words.length < 2)
  399. {
  400. JOptionPane.showMessageDialog(null, "You have only entered one word , It is the longest and shortest word.");
  401. isCorrectFormat = true;
  402. }
  403.  
  404. else
  405. {
  406. for(int i = 0; i < words.length ; i++)
  407. {
  408. if(words[i].length() > l)
  409. l = words[i].length(); //Finds length of the longest word
  410.  
  411. if(words[i].length() < s)
  412. s = words[i].length(); //Finds length of the shortest word
  413. }
  414.  
  415.  
  416.  
  417. for(int q = 0 ; q < words.length; q++)
  418. {
  419. for(int j = q + 1; j < words.length; j++)
  420. {
  421. if (words[q].equals(words[j])) //Checks for duplicate words
  422. words[j] = ""; //If duplicate is found it is set to an empty string
  423. }
  424. }
  425.  
  426. for(int m = 0; m < words.length; m++)
  427. {
  428. if(words[m].length() == l)
  429. longWord += words[m] + " "; //Adds words with same length as the longest word to a String
  430.  
  431. if(words[m].length() == s)
  432. shortWord += words[m] + " "; //Adds words with same length as the shortest word to a String
  433. }
  434. isCorrectFormat = true;
  435.  
  436. JOptionPane.showMessageDialog(null,"Length of longest word(s): " + l + "\nLongest word(s): \t" + longWord +"\nLength of shortest word(s)" + s +"\nShortest word(s): \t" + shortWord);
  437. }
  438. }
  439. }
  440.  
  441.  
  442.  
  443.  
  444.  
  445. public static void areWordsOrPhrasesAnagrams()
  446. {
  447. boolean validInput = false;
  448. while(!validInput)
  449. {
  450.  
  451. String orig1 = JOptionPane.showInputDialog(null,"Please Enter the first word, phrase or sentence\nNumbers and characters will be ignored"); //takes first string
  452. if(orig1==null)return; //cancel->return to main
  453. String orig2 = JOptionPane.showInputDialog(null,"Please Enter the second word, phrase or sentence\nNumbers and characters will be ignored"); //takes second string
  454. if(orig2==null)return; //cancel->return to main
  455. String input1= orig1.replaceAll("[^a-zA-Z]",""); //replaces everything but vowels
  456. String input2= orig2.replaceAll("[^a-zA-Z]",""); //replaces everything but vowels
  457. input1 = input1.toLowerCase(); //converts to lower
  458. input2 = input2.toLowerCase(); //converts to lower
  459.  
  460. char [] chars1 = (input1.toCharArray()); //puts chars into array
  461. char [] chars2 = (input2.toCharArray()); //puts chars into array
  462.  
  463. if(input1.equals("")||input2.equals("")) //error for empty string
  464. JOptionPane.showMessageDialog(null,"Please Enter A valid word/phrase, containing only letters in the English Alphabet","Error",3);
  465. else //continues if valid
  466. {
  467. Arrays.sort(chars1); //sorts array
  468. Arrays.sort(chars2);
  469.  
  470. if(Arrays.equals(chars1, chars2)) //equal arrays(prints true)
  471. {
  472. JOptionPane.showMessageDialog(null,orig1 + " Is an anagram of " + orig2);
  473. validInput=true;
  474. }
  475. else //non equal prints false
  476. {
  477. JOptionPane.showMessageDialog(null,orig1 + " Is not an anagram of " + orig2);
  478. validInput=true;
  479. }
  480. }
  481. }
  482. }
  483.  
  484.  
  485.  
  486.  
  487.  
  488. public static void determineIfPalindromes()
  489. {
  490. boolean flag1 = true, flag2 = true, format=true;
  491. int n=0, i;
  492. String userInput, input [];
  493. String compare = "", compare2 = "";
  494. do
  495. {
  496. userInput = JOptionPane.showInputDialog(null, "Enter word, phrases, or sentence.", "Is the word/phrase/sentence a palindrome?", 3);
  497. if (userInput == null) return;
  498. //int n=0, i;
  499. //String input [];
  500. // String compare = "", compare2 = "";
  501. userInput = userInput.replaceAll("[^a-zA-Z\\s]", "");
  502. if(userInput.equals(""))
  503. {
  504. JOptionPane.showMessageDialog(null,"Please enter a valid word/sentence/phrase.");
  505. format=false;
  506. }
  507. else
  508. format=true;
  509. } while(!format);
  510. input = userInput.split(" ");
  511. n = input.length;
  512. if (input.length>1)
  513. {
  514. for(i = 0; (i< n/2) && (flag1); i++)
  515. {
  516. compare = input[i];
  517. compare2 = input[n-i-1];
  518. if (!(compare.equalsIgnoreCase(compare2))) flag1 = false;
  519. System.out.println(flag1);
  520. }
  521. }
  522. else flag1 = false;
  523. userInput = userInput.replaceAll("[^a-zA-Z]", "");
  524. n = userInput.length();
  525. for(i = 0; (i < n/2) && (flag2 = true); i++)
  526. if (userInput.charAt(i) != userInput.charAt(n-i-1)) flag2 = false;
  527. if(flag1 || flag2) JOptionPane.showMessageDialog(null, userInput+" is a palindrome","Result",3);
  528. else JOptionPane.showMessageDialog(null, userInput+" is not a palindrome","Result",3);
  529. }
  530. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement