Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.46 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 2: countConsonants(); break;
  21. case 3: characterContent(); break;
  22. case 4: keyboardRows(); break;
  23. case 5: alternatingVowelsAndConsonants(); break;
  24. case 6: determineLongestAndShortestWords(); break;
  25. case 7: anagramChecker(); break;
  26. case 8: palindrome(); break;
  27. }
  28. }
  29. }
  30. }
  31. }
  32.  
  33. public static String getMenuOption()
  34. {
  35. String menuOptions = "1. Analyze vowel content of word/phrase." +
  36. "\n2. Analyze consonant content of word/phrase" +
  37. "\n3. Analyze character content of word/phrase" +
  38. "\n4. Determine which row(s) of keys on QWERTY keyboard need to be used to type word/phrase" +
  39. "\n5. Analyze vowel and consonant content of word/phrase" +
  40. "\n6. Length of shortest/longest word and matches of each." +
  41. "\n7. Are two words/phrases anagrams of each other ?" +
  42. "\n8. Is word/phrase a palindrome ?" +
  43. "\n0. Exit.";
  44. String menuMessage = "Choose number of option you wish to have executed";
  45. String errorMessage = "Invalid menu selection.\n\nValid options are 0 to 8 inclusive.";
  46. errorMessage += "\nSelect OK to retry.";
  47. String errorHeader = "Error in user input";
  48. boolean validInput = false;
  49. String selection = "", menuChoicePattern = "[0-8]{1}";
  50.  
  51. while (!(validInput))
  52. {
  53. selection = JOptionPane.showInputDialog(null, menuOptions, menuMessage, 3);
  54. if (selection == null || selection.matches(menuChoicePattern))
  55. validInput = true;
  56. else
  57. JOptionPane.showMessageDialog(null, errorMessage, errorHeader, 2);
  58. }
  59. return selection;
  60. }
  61.  
  62. // John Long-12132306
  63. public static void analyzeVowelContentOfWordPhrase()
  64. {
  65. String validation[] = new String[4];
  66. validation[0] = "Enter word/phrase/sentence";
  67. validation[1] = "Vowel content analyzer";
  68. validation[2] = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  69. validation[3] = "Invalid input.";
  70. validation[3] += "\n\nEnter a word or phrase comprising ";
  71. validation[3] += "alphabetic/numeric characters only.";
  72. validation[3] += "\nSelect OK to retry.";
  73.  
  74. String vowels = "aeiou";
  75. String input = getWordOrPhraseFromEndUser(validation);
  76. String tempInput = input.toLowerCase();
  77. tempInput = tempInput.replaceAll(" ","");
  78.  
  79. boolean allPresent = false;
  80. String report = "";
  81. String aVowel;
  82. int vCount [] = {0,0,0,0,0};
  83. char vChar;
  84. String vowelPtrn = "[^aeiou]+";
  85. String alphaPattern = "[^aeiou]*[a]+[^aeiou]*[e]{1}[^aeiou]*[i]{1}[^aeiou]*[o]{1}[^aeiou]*[u]{1}[^aeiou]*";
  86. String reverseAlphaPattern = "[^aeiou]*[u]+[^aeiou]*[o]{1}[^aeiou]*[i]{1}[^aeiou]*[e]{1}[^aeiou]*[a]{1}[^aeiou]*";
  87. boolean inOrder = false;
  88.  
  89. // check for any vowels
  90. if(tempInput.matches(vowelPtrn))
  91. report += "Input contains no vowels\n";
  92.  
  93. // check for all vowels presence
  94. for(int i=0; i<vowels.length(); i++)
  95. {
  96. aVowel = vowels.substring(i,i+1);
  97. if(tempInput.indexOf(aVowel) == -1)
  98. allPresent = false;
  99. else
  100. allPresent = true;
  101. }
  102.  
  103.  
  104. report += "Vowel frequencies are\n";
  105. // count vowels for which there is at least 1 occurrence
  106. for(int i=0; i<vowels.length(); i++)
  107. {
  108. vChar = vowels.charAt(i);
  109. for(int j=0; j<tempInput.length(); j++)
  110. {
  111. if(vChar == tempInput.charAt(j))
  112. vCount[i]++;
  113. }
  114.  
  115. if(vCount[i] > 0)
  116. report += vowels.substring(i,i+1) + " = "+ vCount[i] + "\n";
  117. }
  118.  
  119. // check for alphabetic normal+reverse order
  120. if(allPresent)
  121. {
  122. report += "All vowels are present\n";
  123.  
  124. if(tempInput.matches(alphaPattern))
  125. report += "Input contains vowels in alphabetic order";
  126.  
  127. if(tempInput.matches(reverseAlphaPattern))
  128. report += "Input contains vowels in reverse alphabetic order";
  129.  
  130. }else
  131. report += "All vowels not present\n";
  132.  
  133. JOptionPane.showMessageDialog(null,report,"Report",1);
  134. }
  135.  
  136.  
  137. // John Long-12132306
  138. public static void determineLongestAndShortestWords()
  139. {
  140. String validation[] = new String[4];
  141. validation[0] = "Enter word/phrase/sentence";
  142. validation[1] = "Word length/match analyzer";
  143. validation[2] = "([a-zA-Z0-9]+)|((([a-zA-Z0-9]+\\s)+)[a-zA-Z0-9]+)";
  144. validation[3] = "Invalid input.";
  145. validation[3] += "\n\nEnter a word or phrase comprising ";
  146. validation[3] += "alphabetic/numeric characters only.";
  147. validation[3] += "\nSelect OK to retry.";
  148.  
  149.  
  150. String input = getWordOrPhraseFromEndUser(validation);
  151. String tempInput = input.toLowerCase();
  152. tempInput = tempInput.trim(); // for leading or trailing spaces
  153. String words [] = tempInput.split(" ");
  154. String report = "";
  155. String longestMatches = "";
  156. String shortestMatches = "";
  157.  
  158. // assume 1st word is longest and shortest
  159. // longword/shortword are placeholders for the longest/shortest words
  160. String longWord = words[0];
  161. String shortWord = words[0];
  162.  
  163. int longest = longWord.length();
  164. int shortest = shortWord.length();
  165.  
  166. // iterate to find longest and shortest sizes first
  167. for(int i=0; i<words.length; i++)
  168. {
  169. // compare and update longest/shortest word
  170. if(words[i].length() >= longest)
  171. {
  172. longWord = words[i];
  173. longest = longWord.length();
  174. }
  175. else if(words[i].length() <= shortest)
  176. {
  177. shortWord = words[i];
  178. shortest = shortWord.length();
  179. }
  180. }
  181.  
  182. // iterate to find matches for shortest/longest sizes in input
  183. for(int i=0; i<words.length; i++)
  184. {
  185. // if word matches longest/shortest then populate appropriate array
  186. if(words[i].length() == longest)
  187. longestMatches += words[i] + " ";
  188.  
  189. else if(words[i].length() == shortest)
  190. shortestMatches += words[i] + " ";
  191. }
  192.  
  193. report += "Max length = " + longest + "\n" + "Matching words are\n" + longestMatches + "\n";
  194. report += "Min length = " + shortest + "\n" + "Matching words are\n" + shortestMatches + "\n";
  195.  
  196. JOptionPane.showMessageDialog(null,report,"Report",1);
  197. }
  198.  
  199.  
  200.  
  201.  
  202.  
  203. public static void keyboardRows()
  204. {
  205. //Jamie McLoughlin
  206. String tempInput = "";
  207. String validation[] = new String[4];
  208. validation[0] = "Please enter words/phrases \n with alphabetic characters only.";
  209. validation[1] = "Alternating Vowels and Consonants.";
  210. validation[2] = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  211. validation[3] = "Invalid input.";
  212. validation[3] += "\n\nEnter a word or phrase comprising ";
  213. validation[3] += "alphabetic characters only.";
  214. validation[3] += "\nSelect OK to retry.";
  215.  
  216. //Get word from end used and cheak to see if it only Alphabetic Characters Only.
  217. String userInput = getWordOrPhraseFromEndUser(validation);
  218. String message = "To tpye \"" + userInput + "\" you need to use";
  219.  
  220. //Get rid of any white spaces and turn any uppercase to lowercase.
  221. tempInput = userInput.replaceAll("[\\s]", "");
  222. tempInput = tempInput.toLowerCase();
  223.  
  224. /*This regular expression starts with 0 or more character followed by 1 or more of the first row letter .
  225. Then by 0 or more character all this happens 1 or more time. then its followed by the same thing 2 time more but for
  226. letters in row2 and in row3. */
  227. String pattern1 = "(\\w*[qwertyuiop]+\\w*)+(\\w*[asdfghjkl]+\\w*)+(\\w*[zxcvbnm]+\\w*)+";
  228. String pattern2 = "\\w*[qwertyuiop]+\\w*";
  229. String pattern3 = "\\w*[asdfghjkl]+\\w*";
  230. String pattern4 = "\\w*[zxcvbnm]+\\w*";
  231.  
  232. //Checks the regular expression matches the user's input.
  233. if (tempInput.matches(pattern1))
  234. message = "\nAll rows are need 1. 2. and 3.";
  235.  
  236. else {
  237. if (tempInput.matches(pattern2))
  238. message += "\nRow 1.";
  239.  
  240. if (tempInput.matches(pattern3))
  241. message += "\nRow 2.";
  242.  
  243. if (tempInput.matches(pattern4))
  244. message += "\nRow 3.";
  245. }
  246. JOptionPane.showMessageDialog(null, message, validation[1], 1);
  247. }
  248. public static void alternatingVowelsAndConsonants()
  249. {
  250. //Jamie McLoughlin
  251. String tempInput = "";
  252. String validation[] = new String[4];
  253. validation[0] = "Please enter words/phrases \n with alphabetic characters only.";
  254. validation[1] = "Alternating Vowels and Consonants.";
  255. validation[2] = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  256. validation[3] = "Invalid input.";
  257. validation[3] += "\n\nEnter a word or phrase comprising ";
  258. validation[3] += "alphabetic characters only.";
  259. validation[3] += "\nSelect OK to retry.";
  260.  
  261. //Get word from end used and cheak to see if it only Alphabetic Characters Only.
  262. String userInput = getWordOrPhraseFromEndUser(validation);
  263. String message1 = userInput + "\n\nDoes include\nAlternating Vowels and Consonants.";
  264. String message2 = userInput + "\n\nDoes not include\nAlternating Vowels and Consonants.";
  265.  
  266. //Get rid of any white spaces and turn any uppercase to lowercase.
  267. tempInput = userInput.replaceAll("[\\s]", "");
  268. tempInput = tempInput.toLowerCase();
  269.  
  270. /*This regular expression starts with a Consonants followed by a Vowels and alternating until it either ends with
  271. or without a Consonants or it will do the complete opposite starting with a Vowel.*/
  272. String pattern = "(([^aeiou][aeiou])+[^aeiou]?)|(([aeiou][^aeiou])+[aeiou]?)";
  273.  
  274. //Checks the regular expression matches the user's input.
  275. if (tempInput.matches(pattern))
  276. JOptionPane.showMessageDialog(null, message1, validation[1], 1);
  277. else
  278. JOptionPane.showMessageDialog(null, message2, validation[1], 1);
  279. }
  280.  
  281. //Daire Lavelle-16192249
  282. public static void characterContent()
  283. {
  284. String validation[] = new String[4];
  285. validation[0] = "Enter a word/phrase to have its character content analysed";
  286. validation[1] = "Character Content";
  287. validation[2] = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  288. validation[3] = "Invalid input.";
  289. validation[3] += "\n\nEnter a word or phrase comprising ";
  290. validation[3] += "alphabetic characters only.";
  291. validation[3] += "\nSelect OK to retry.";
  292.  
  293. String phrase=getWordOrPhraseFromEndUser(validation);
  294. String out="";
  295. String symbols="";
  296. int numberOfDigits=0;
  297. int numberOfLetters=0;
  298. int numberOfSymbols=0;
  299. int[] digitFrequency=new int[10];
  300. int[] letterFrequency=new int[52];
  301. boolean symbolBool=false, isValidInput=true;
  302.  
  303. //all input is valid except an empty string
  304. if (phrase==null)
  305. isValidInput=false;
  306. if (isValidInput)
  307. {
  308. phrase=phrase.trim();
  309. for (int i=0; i<phrase.length(); i++) //loop through each character in the phrase
  310. {
  311. char currentChar=phrase.charAt(i);
  312. if (Character.getType(currentChar)==9)
  313. {
  314. //code for decimal digit
  315. numberOfDigits++;
  316. digitFrequency[(int)currentChar-48]++;
  317. }
  318. else if ((int)currentChar>=97 && (int)currentChar<=122)
  319. {
  320. //code for a lower case letter
  321. numberOfLetters++;
  322. letterFrequency[(int)currentChar-97]++;
  323. }
  324. else if ((int)currentChar>=65 && (int)currentChar<=90)
  325. {
  326. //code for upper case letter
  327. numberOfLetters++;
  328. letterFrequency[(int)currentChar-39]++;
  329. }
  330. else if ((int)currentChar!=32)
  331. {
  332. //code for other symbol
  333. symbols+=currentChar; //creates a string of just the symbols
  334. numberOfSymbols++;
  335. }
  336. }
  337.  
  338. char[] symbolArray=symbols.toCharArray();
  339. int[] symbolFrequency=new int[symbolArray.length];
  340. //The frequency of the first occurence of a symbol in symbolArray will be
  341. //stored in the corresponding index of symbolFrequency. When the output
  342. //String is constructed, it will ignore any symbols whose frequency is 0
  343. for (int i=0; i<symbols.length(); i++)
  344. {
  345. int j=0;
  346. while (symbolArray[j]!=(symbolArray[i]))
  347. j++;
  348. symbolFrequency[j]++;
  349. }
  350.  
  351. //The rest of the code constructs the output String
  352. if (numberOfLetters>0)
  353. {
  354. out+="Number of letters: "+numberOfLetters+"\nFrequency of letters:\n";
  355. for (int i=0; i<26; i++)
  356. {
  357. if (letterFrequency[i]!=0)
  358. out+=String.format("%1$c:%2$3d\n", (char)(i+97), letterFrequency[i]);
  359. }
  360. for (int i=26; i<letterFrequency.length; i++)
  361. {
  362. if (letterFrequency[i]!=0)
  363. out+=String.format("%1$c:%2$3d\n", (char)(i+39), letterFrequency[i]);
  364. }
  365. }
  366.  
  367. if (numberOfDigits>0)
  368. {
  369. out+="Number of digits: "+numberOfDigits+"\nFrequency of digits:\n";
  370. for (int i=0; i<digitFrequency.length; i++)
  371. {
  372. if (digitFrequency[i]!=0)
  373. out+=String.format("%1$c:%2$3d\n", i, digitFrequency[i]);
  374. }
  375. }
  376.  
  377. if (numberOfSymbols>0)
  378. {
  379. out+="Number of other symbols: "+numberOfSymbols+"\nFrequency of other symbols:\n";
  380. for (int i=0; i<symbolFrequency.length; i++)
  381. {
  382. if (symbolFrequency[i]!=0)
  383. out+=String.format("%1$c:%2$3d\n", symbolArray[i], symbolFrequency[i]);
  384. }
  385. }
  386. JOptionPane.showMessageDialog(null, out, "Character Frequency",1);
  387. }
  388. else
  389. JOptionPane.showMessageDialog(null, "Invalid input. A word or phrase is required.", "Error", 2);
  390. }
  391.  
  392.  
  393. //Daire Lavelle-16192249
  394. public static void palindrome()
  395. {
  396. String validation[] = new String[4];
  397. validation[0] = "The program will determine whether or not the phrase you enter is a palindrome";
  398. validation[1] = "Palindrome";
  399. validation[2] = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  400. validation[3] = "Invalid input.";
  401. validation[3] += "\n\nEnter a word or phrase comprising ";
  402. validation[3] += "alphabetic/numeric characters only.";
  403. validation[3] += "\nSelect OK to retry.";
  404.  
  405. String phrase=getWordOrPhraseFromEndUser(validation);
  406. phrase = phrase.toLowerCase();
  407. String phraseLetters="";
  408. String phraseWords;
  409. String out="";
  410. String[] phraseWordsArray;
  411. char currentChar;
  412. boolean lettersArePalindrome=true, wordsArePalindrome=true, stop=false, isValidInput=true;
  413.  
  414. phrase=phrase.trim();
  415. //make a new string without symbols, numbers or whitespace.
  416. phraseLetters=phrase.replaceAll("\\s","");
  417. //compare the first letter to the last, then the second letter to the second last
  418. //until they're not the same
  419. char[] phraseLettersArray=phraseLetters.toCharArray();
  420. int i=0;
  421. while(phraseLettersArray[i]==phraseLettersArray[phraseLettersArray.length-i-1] && i<=(phraseLettersArray.length)/2)
  422. {
  423. i++;
  424. }
  425. //if the letters were all the same by the time the middle characters were
  426. //checked, then it's a palindrome. No need to compare characters a second
  427. //time.
  428. if (!(i>=(phraseLettersArray.length)/2))
  429. lettersArePalindrome=false;
  430. phraseWords=phrase.replaceAll("[^a-z^A-Z^0-9^\\s]",""); //remove all punctuation from the phrase
  431. phraseWords=phrase.replaceAll("\\s+"," ");
  432. phraseWordsArray=phraseWords.split(" "); //split the phrase into an array of words
  433. //if the array is of length 1, the while will attempt to comare the 0th
  434. // index with the -1st index which crashes
  435. if (phraseWordsArray.length>1)
  436. {
  437. i=0;
  438. while(phraseWordsArray[i].equals(phraseWordsArray[phraseWordsArray.length-i-1]) && i<=(phraseWordsArray.length)/2)
  439. {
  440. i++;
  441. }
  442. if (!(i>=(phraseWordsArray.length)/2))
  443. wordsArePalindrome=false;
  444. }
  445. else
  446. wordsArePalindrome=false;
  447. //construct the output String
  448. if (lettersArePalindrome)
  449. out+="This phrase's letters form a palindrome.\n";
  450. if (wordsArePalindrome && phraseWordsArray.length>1)
  451. out+="This phrases's words form a palindrome.\n";
  452. if (!wordsArePalindrome && !lettersArePalindrome)
  453. out+="This phrse is not a palindrome.\n";
  454. JOptionPane.showMessageDialog(null, out, "Is the phrase a palidrome",1);
  455. }
  456.  
  457. public static void countConsonants()
  458. {
  459.  
  460. String validation[] = new String[4];
  461. validation[0] = "The program will analyze consonant content of input";
  462. validation[1] = "Count Consonants";
  463. validation[2] = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  464. validation[3] = "Invalid input.";
  465. validation[3] += "\n\nEnter a word or phrase comprising ";
  466. validation[3] += "alphabetic/numeric characters only.";
  467. validation[3] += "\nSelect OK to retry.";
  468.  
  469. String word = getWordOrPhraseFromEndUser(validation);
  470. int length = word.length();
  471. int[] frequency = new int[length];
  472. int[] cons = new int[length];
  473. String result = "";
  474. int count = 0;
  475. int car;
  476.  
  477. word = word.toLowerCase(); //The word is changed to lowercase
  478. word = word.replaceAll("[\\W]",""); //The word is left with letters only
  479.  
  480. for(int i = 0; i<length;i++)
  481. {
  482. car=(int)word.charAt(i); //stores character at index i in temp variable 'car'
  483. if(!(car == 97 ||car == 101 ||car == 105||car == 111||car == 117)) //if the char is not a vowel put it into the array cons
  484. {
  485. cons[count] = car;
  486. count++;
  487. }
  488. }
  489.  
  490. sorting(cons);
  491. frequency = counting(cons,frequency);
  492.  
  493. for(int i = 0;i<cons.length;i++) //For the amount of consonants
  494. {
  495. if (cons[i]!=0 && frequency[i]!=0) //Due to the counting method this if is necessary
  496. result += ((char)cons[i] + " " + frequency[i] + " time(s)\n");
  497. }
  498.  
  499. JOptionPane.showMessageDialog(null,result);
  500. }
  501. public static void sorting(int[] cons) //This method sorts the characters using their ASCII code
  502. { //Bubble sort
  503. int pass, comparison, temp;
  504. boolean sorted = false;
  505. for (pass = 1; pass <= (cons.length - 1) && !sorted; pass++)
  506. {
  507. sorted = true;
  508. for (comparison = 1; comparison <= (cons.length - pass); comparison++)
  509. {
  510. if (cons[comparison - 1] < cons[comparison])
  511. {
  512. temp = cons[comparison - 1];
  513. cons[comparison - 1] = cons[comparison];
  514. cons[comparison] = temp;
  515. sorted = false;
  516. }
  517. }
  518. }
  519. }
  520. public static int[] counting(int[] cons,int[] frequency) //This method counts the frequency of the consonants and returns that array
  521. {
  522. for (int i = 0; (i < cons.length - 1); i++)
  523. {
  524. int j=0;
  525. while (cons[j]!=cons[i] && cons[j]!=0)
  526. j++;
  527. frequency[j]++;
  528. }
  529. return frequency;
  530. }
  531.  
  532.  
  533. public static void anagramChecker()
  534. {
  535. String validation[] = new String[4];
  536. validation[0] = "Enter a word/phrase to check if it's an anagram";
  537. validation[1] = "Anagram Checker";
  538. validation[2] = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
  539. validation[3] = "Invalid input.";
  540. validation[3] += "\n\nEnter a word or phrase comprising ";
  541. validation[3] += "alphabetic characters only.";
  542. validation[3] += "\nSelect OK to retry.";
  543.  
  544. String one = getWordOrPhraseFromEndUser(validation);
  545. String two = getWordOrPhraseFromEndUser(validation);
  546.  
  547. one = one.toLowerCase();
  548. two = two.toLowerCase(); //puts both strings into lower case
  549.  
  550. one = one.replaceAll("[\\W]","");
  551. two = two.replaceAll("[\\W]",""); //replaces both strings with letters only
  552.  
  553. one=sorting(one);
  554. two=sorting(two); //sorts strings by character ASCII code
  555.  
  556. boolean check = check(one,two); //simple method to make check more efficient
  557. if(check && one.equals(two)) //compares both arrays nad if they are equal they are anagrams
  558. JOptionPane.showMessageDialog(null,"These words/phrases are anagrams of each other");
  559. else
  560. JOptionPane.showMessageDialog(null,"These are not anagrams");
  561.  
  562. }
  563.  
  564. public static boolean check(String one,String two)
  565. {
  566. if(one.length() != two.length())
  567. return false;
  568. else
  569. return true;
  570. }
  571.  
  572. public static String sorting(String word) //bubble sort to organise characters by their ASCII code
  573. {
  574. int pass, comparison;
  575. char temp;
  576. boolean sorted = false;
  577. char[] chars = word.toCharArray();
  578. for (pass = 1; pass <= (chars.length - 1) && !sorted; pass++)
  579. {
  580. sorted = true;
  581. for (comparison = 1; comparison <= (chars.length - pass); comparison++)
  582. {
  583. if (chars[comparison - 1] < chars[comparison])
  584. {
  585. temp = chars[comparison - 1];
  586. chars[comparison - 1] = chars[comparison];
  587. chars[comparison] = temp;
  588. sorted = false;
  589. }
  590. }
  591. }
  592. String stringSorted = new String(chars);
  593. return stringSorted;
  594. }
  595.  
  596.  
  597.  
  598. public static String getWordOrPhraseFromEndUser(String validation[])
  599. {
  600. /* Jamie McLoughlin
  601. validation [0] = The Window Message.
  602. validation [1] = The Window Title.
  603. validation [2] = The Regular expression.
  604. validation [3] = The Error Message.
  605. */
  606. boolean validInput = false;
  607. String userInput = "";
  608.  
  609. while (!(validInput))
  610. {
  611. userInput = JOptionPane.showInputDialog(null, validation[0], validation[1], 3);
  612. if (userInput == null || userInput.matches(validation[2]))
  613. validInput = true;
  614. else
  615. JOptionPane.showMessageDialog(null, validation[3], "Error in user input", 2);
  616. }
  617.  
  618. return userInput;
  619. }
  620.  
  621.  
  622. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement