Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class Analyser
- {
- public static void main(String [] args)
- {
- int choice;
- String menuOption = "";
- while ((menuOption != null) && (!(menuOption.equals("0"))))
- {
- menuOption = getMenuOption();
- if (menuOption != null)
- {
- choice = Integer.parseInt(menuOption);
- if (choice != 0)
- {
- switch(choice)
- {
- case 1: analyzeVowelContentOfWordPhrase(); break;
- case 4: keyboardRows(); break;
- case 5: alternatingVowelsAndConsonants(); break;
- case 6: determineLongestAndShortestWords(); break;
- }
- }
- }
- }
- }
- public static String getMenuOption()
- {
- String menuOptions = "1. Analyze vowel content of word/phrase." +
- "\n2. Analyze consonant content of word/phrase" +
- "\n3. Analyze character content of word/phrase" +
- "\n4. Determine which row(s) of keys on QWERTY keyboard need to be used to type word/phrase" +
- "\n5. Analyze vowel and consonant content of word/phrase" +
- "\n6. Length of shortest/longest word and matches of each." +
- "\n7. Are two words/phrases anagrams of each other ?" +
- "\n8. Is word/phrase a palindrome ?" +
- "\n0. Exit.";
- String menuMessage = "Choose number of option you wish to have executed";
- String errorMessage = "Invalid menu selection.\n\nValid options are 0 to 8 inclusive.";
- errorMessage += "\nSelect OK to retry.";
- String errorHeader = "Error in user input";
- boolean validInput = false;
- String selection = "", menuChoicePattern = "[0-7]{1}";
- while (!(validInput))
- {
- selection = JOptionPane.showInputDialog(null, menuOptions, menuMessage, 3);
- if (selection == null || selection.matches(menuChoicePattern))
- validInput = true;
- else
- JOptionPane.showMessageDialog(null, errorMessage, errorHeader, 2);
- }
- return selection;
- }
- // John Long-12132306
- public static void analyzeVowelContentOfWordPhrase()
- {
- String msg1 = "Vowel content analyzer";
- String msg2 = "Enter word/phrase";
- String vowels = "aeiou";
- String input = getWordOrPhraseFromEndUser(msg1,msg2);
- String tempInput = input.toLowerCase();
- tempInput = tempInput.trim(); // for leading or trailing spaces
- tempInput = tempInput.replaceAll(" ","");
- boolean allPresent = false;
- String report = "";
- String aVowel;
- int vCount [] = {0,0,0,0,0};
- char vChar;
- String vowelPtrn = "^[aeiou]";
- // check for any vowels
- if(tempInput.matches(vowelPtrn))
- report += "Input contains no vowels\n";
- // check for all vowels presence
- for(int i=0; i<vowels.length(); i++)
- {
- aVowel = vowels.substring(i,i+1);
- if(tempInput.indexOf(aVowel) == -1)
- allPresent = false;
- else
- allPresent = true;
- }
- // *** count vowels for which there is at least 1 occurrence
- for(int i=0; i<vowels.length(); i++)
- {
- vChar = vowels.charAt(i);
- for(int j=0; j<tempInput.length(); j++)
- {
- if(vChar == tempInput.charAt(j))
- vCount[i]++;
- }
- }
- //report += "Vowel frequencies are\n"+"a="+vCount[0]+"\n"+"e="+vCount[1]+"\n"+"i="+vCount[2]+"\n"+"o="+vCount[3]+"\n"+"u="+vCount[4];
- // check for alphabetic normal+reverse order
- if(allPresent)
- {
- report += "All vowels are present\n";
- }else
- report += "All vowels not present\n";
- JOptionPane.showMessageDialog(null,report,"Report",1);
- }
- // John Long-12132306
- public static void determineLongestAndShortestWords()
- {
- String msg1 = "Word length/match analyzer";
- String msg2 = "Enter word/phrase/sentence";
- String input = getWordOrPhraseFromEndUser(msg1,msg2);
- String tempInput = input.toLowerCase();
- tempInput = tempInput.trim(); // for leading or trailing spaces
- String words [] = tempInput.split(" ");
- String report = "";
- String longestMatches = "";
- String shortestMatches = "";
- // assume 1st word is longest and shortest
- // longword/shortword are placeholders for the longest/shortest words
- String longWord = words[0];
- String shortWord = words[0];
- int longest = longWord.length();
- int shortest = shortWord.length();
- // iterate to find longest and shortest sizes first
- for(int i=0; i<words.length; i++)
- {
- // compare and update longest/shortest word
- if(words[i].length() > longest)
- longWord = words[i];
- else if(words[i].length() < shortest)
- shortWord = words[i];
- }
- // iterate to find matches for shortest/longest sizes in input
- for(int i=0; i<words.length; i++)
- {
- // if word matches longest/shortest then populate appropriate array
- if(words[i].length() == longest)
- longestMatches += words[i] + " ";
- else if(words[i].length() == shortest)
- shortestMatches += words[i] + " ";
- }
- report += "Max length = " + longest + "\n" + "Matching words are\n" + longestMatches + "\n";
- report += "Min length = " + shortest + "\n" + "Matching words are\n" + shortestMatches + "\n";
- JOptionPane.showMessageDialog(null,report,"Report",1);
- }
- public static void alternatingVowelsAndConsonants()
- {
- //Jamie McLoughlin
- String tempInput = "";
- String windowMessagew = "Please enter words/phrases \n with alphabetic characters only.";
- String windowTitle = "Alternating Vowels and Consonants.";
- //Get word from end used and cheak to see if it only Alphabetic Characters Only.
- String userInput = getWordOrPhraseFromEndUser(windowMessagew,windowTitle);
- String message1 = userInput + " does have alternating Vowels and Consonants.";
- String message2 = userInput + " does not have alternating Vowels and Consonants.";
- //Get rid of any white spaces and turn any uppercase to lowercase
- tempInput = userInput.replaceAll("[\\s]","");
- tempInput = tempInput.toLowerCase();
- /*The pattern starts with a Consonants followed by a Vowels and alternating until it either ends with or without a Consonants
- or the opposite starting with a Vowel.*/
- String pattern = "(([^aeiou][aeiou])+[^aeiou]?)|(([aeiou][^aeiou])+[aeiou]?)";
- //Just checked to see if the word matches the pattern and Returns a message if it does or not.
- if (tempInput.matches(pattern))
- {
- JOptionPane.showMessageDialog(null, message1, windowTitle, 1);
- }
- else
- {
- JOptionPane.showMessageDialog(null, message2, windowTitle, 1);
- }
- }
- public static void keyboardRows()
- {
- //Jamie McLoughlin
- String windowMessagew = "Please enter words/phrases \n with alphabetic characters only.";
- String windowTitle = "Keyboard rows.";
- String tempInput = "";
- String message ="You need to use rows";
- String message2 = " 1.", message3 = " 2.", message4 = " 3.";
- String row1 = "[qwertyuiop]", row2 = "[asdfghjkl]";
- int i = 0;
- boolean useRow1 = false,useRow2 = false,useRow3 = false,useAllRows = false;
- //Get word from end used and cheak to see if it only Alphabetic Characters Only.
- String userInput = getWordOrPhraseFromEndUser(windowMessagew,windowTitle);
- //Get rid of any white spaces and turn any uppercase to lowercase
- tempInput = userInput.replaceAll("[\\s]","");
- tempInput = tempInput.toLowerCase();
- /*Go through the while loop and if the Alphabetic Characters matches the Regular Expression
- for that row it turns the boolean to true. If all rows are true it stops looking.
- Then shows the user the rows needed to use*/
- while (i < tempInput.length() && !useAllRows)
- {
- if ((tempInput.substring(i,i+1)).matches(row1))
- {
- if (!useRow1){
- message += message2;
- }useRow1 = true;
- }
- else if ((tempInput.substring(i,i+1)).matches(row2))
- {
- if (!useRow2){
- message += message3;
- }useRow2 = true;
- }
- else
- {
- if (!useRow3){
- message += message4;
- }useRow3 = true;
- }
- if (useRow1 && useRow2 && useRow3)
- {
- useAllRows = true;
- }
- i++;
- }
- message += " to tpye " + userInput;
- JOptionPane.showMessageDialog(null, message, windowTitle, 1);
- }
- public static String getWordOrPhraseFromEndUser(String windowMessage, String windowTitle)
- {
- boolean validInput = false;
- String userInput = "";
- String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
- String errorMessage = "Invalid input.";
- errorMessage += "\n\nEnter a word or phrase comprising ";
- errorMessage += "alphabetic characters only.";
- errorMessage += "\nSelect OK to retry.";
- while (!(validInput))
- {
- userInput = JOptionPane.showInputDialog(null, windowMessage, windowTitle, 3);
- if (userInput == null || userInput.matches(pattern))
- validInput = true;
- else
- JOptionPane.showMessageDialog(null, errorMessage, "Error in user input", 2);
- }
- return userInput;
- }
- public static String getWordOrPhraseFromEndUserLtrsandNums(String windowMessage, String windowTitle)
- {
- boolean validInput = false;
- String userInput = "";
- String pattern = "(([a-zA-Z0-9])+)|(((([a-zA-Z0-9])+\\s)+)([a-zA-Z0-9])+)";
- String errorMessage = "Invalid input.";
- errorMessage += "\n\nEnter a word or phrase comprising ";
- errorMessage += "alphabetic and numeric characters only.";
- errorMessage += "\nSelect OK to retry.";
- while (!(validInput))
- {
- userInput = JOptionPane.showInputDialog(null, windowMessage, windowTitle, 3);
- if (userInput == null || userInput.matches(pattern))
- validInput = true;
- else
- JOptionPane.showMessageDialog(null, errorMessage, "Error in user input", 2);
- }
- return userInput;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement