Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class Project1
- {
- 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: VowelAndConsonant(); break;
- case 2: qwerty(); break;
- case 3: shortestAndLongest(); break;
- case 4: areTheyAnagrams(); break;
- case 5: isItAPalindrome(); break;
- case 6: analyzeCharacterContent(); break;
- }
- }
- }
- }
- }
- public static String getMenuOption()
- {
- String menuOptions = "1. Analyse vowel/consonant content of word/phrase.\n" +
- "2. What keyboard rows were used?\n" +
- "3. Display the shortest and longest words in a sentence.\n" +
- "4. Are two words/phrases anagrams of each other?\n" +
- "5. Is the word/phrase a palindrome?\n" +
- "6. Analyse character content of word/phrase.\n" +
- "0. Exit.\n";
- String menuMessage = "Choose number of option you wish to have executed";
- String errorMessage = "Invalid menu selection.\n\nValid options are 0 to 7 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;
- }
- public static void VowelAndConsonant()
- {
- String message1 = "Choose an option:\n"+
- "1. Analyze vowel and consonant content\n"+
- "2. Check for alternating vowels and consonants";
- String message2 = "Analyze word";
- String pattern = "[1-2]";
- int input = Integer.parseInt(getWordOrPhraseFromEndUser(message1, message2, pattern));
- switch(input)
- {
- case 1: analyzeVowelConsonantContentOfWordPhrase(); break;
- case 2: alternatingVowelsConsonants(); break;
- }
- }
- public static void analyzeVowelConsonantContentOfWordPhrase()
- {
- String message2 = "Analyze vowel and consonant contents";
- String message1 = "Enter a word/phrase";
- String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
- String input = getWordOrPhraseFromEndUser(message1, message2, pattern);
- String result = "";
- String userInput = input.toLowerCase();
- char[] testInput = userInput.toCharArray();
- String[] test = new String[testInput.length];
- for(int j = 0 ; j < testInput.length ; j++)
- {
- char holder = testInput[j];
- test[j] = Character.toString(holder);
- }
- String vowelPattern = "a|e|i|o|u";
- String aPattern = "a";
- String ePattern = "e";
- String iPattern = "i";
- String oPattern = "o";
- String uPattern = "u";
- boolean onlyConsonants = true;
- boolean allVowels = false;
- boolean vowelsOrder = false;
- boolean vowelsBackwards = false;
- int countA = 0;
- int countE = 0;
- int countI = 0;
- int countO = 0;
- int countU = 0;
- int indexA;
- int indexE;
- int indexI;
- int indexO;
- int indexU;
- String consonants = "bcdfghjklmnpqrstvwxyz";
- int[] count = new int[21];
- String[] conOutput = {"B","C","D","F","G","H","J","K","L","M","N","P","Q","R","S","T","V","W","X","Y","Z"};
- String aChar;
- int position;
- String tempInput = userInput.replaceAll("[^a-zA-Z]","");
- String r = "\nConsonant count:";
- for(int i = 0 ; i < tempInput.length() ; i++)
- {
- aChar = tempInput.substring(i, i+1);
- position = consonants.indexOf(aChar);
- if(position != -1)
- count[position]++;
- }
- for(int j = 0 ; j < consonants.length() ; j++)
- {
- if(count[j] != 0)
- r += "\n" + conOutput[j] + " occurs " + count[j] + " time( s)";
- }
- for(int j = 0 ; j < testInput.length ; j++)
- {
- if(test[j].matches(vowelPattern))
- onlyConsonants = false;
- }
- for(int j = 0 ; j < testInput.length ; j++)
- {
- if(test[j].matches(aPattern))
- countA++;
- else if(test[j].matches(ePattern))
- countE++;
- else if(test[j].matches(iPattern))
- countI++;
- else if(test[j].matches(oPattern))
- countO++;
- else if(test[j].matches(uPattern))
- countU++;
- }
- if(countA >= 1 && countE >= 1 && countI >= 1 && countO >= 1 && countU >= 1)
- {
- allVowels = true;
- indexA = input.indexOf('a');
- indexE = input.indexOf('e');
- indexI = input.indexOf('i');
- indexO = input.indexOf('o');
- indexU = input.indexOf('u');
- if(((indexA < indexE)&&(indexE < indexI)&&(indexI < indexO)&&(indexO < indexU)) && ((countA == 1)&&(countE == 1)&&(countI == 1)&&(countO == 1)&&(countU == 1)))
- vowelsOrder = true;
- else if(((indexA > indexE)&&(indexE > indexI)&&(indexI > indexO)&&(indexO > indexU)) && ((countA == 1)&&(countE == 1)&&(countI == 1)&&(countO == 1)&&(countU == 1)))
- vowelsBackwards = true;
- }
- if(onlyConsonants)
- result += "Input contains no vowels";
- else if(vowelsOrder)
- result += "Input contains all vowels in alphabetical order\nVowel count:";
- else if(vowelsBackwards)
- result += "Input contains all vowels in reverse alphabetical order\nVowel count:";
- else if(allVowels)
- result += "Input contains all vowels in any order\nVowel count:";
- else
- result += "Input contains vowels and consonants but not all vowels\nVowel count:";
- if(countA >= 1)
- result += "\n A occurs " + countA + " time(s)";
- if(countE >= 1)
- result += "\n E occurs " + countE + " time(s)";
- if(countI >= 1)
- result += "\n I occurs " + countI + " time(s)";
- if(countO >= 1)
- result += "\n O occurs " + countO + " time(s)";
- if(countU >= 1)
- result += "\n U occurs " + countU + " time(s)";
- result += r;
- JOptionPane.showMessageDialog(null, result);
- }
- public static void alternatingVowelsConsonants()
- {
- String message1 = "Are there alternating vowels and consonants?";
- String message2 = "Enter word/phrase";
- String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
- String input = getWordOrPhraseFromEndUser(message2, message1, pattern);
- String result = "";
- String vowels = "aeiou";
- String consonants = "bcdfghjklmnpqrstvwxyz";
- String tempInput = input.toLowerCase();
- tempInput = tempInput.replaceAll("[^a-z]","");
- String first = tempInput.substring(0,1);
- String[] inputArray = tempInput.split("");
- boolean conTrue = true;
- boolean vowTrue = true;
- int i, j;
- if(vowels.indexOf(first) != -1)
- {
- i = 1;
- j = 2;
- }
- else
- {
- i = 0;
- j = 1;
- }
- while(conTrue && i < inputArray.length)
- {
- if(consonants.indexOf(inputArray[i]) == -1)
- conTrue = false;
- i +=2;
- }
- if(conTrue)
- {
- while(vowTrue && j < inputArray.length)
- {
- if(vowels.indexOf(inputArray[j]) == -1)
- vowTrue = false;
- j += 2;
- }
- }
- if(vowTrue && conTrue)
- result += "Word/phrase entirely comprises of alternating vowels and consonants";
- else
- result += "Word/phrase does not entirely comprise of alternating vowels and consonants";
- JOptionPane.showMessageDialog(null, result);
- }
- public static String qwerty()
- {
- //***
- String message1 = "What keyboard rows are used?";
- String message2 = "Enter word/phrase";
- String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
- String input = getWordOrPhraseFromEndUser(message2, message1, pattern);
- String row1 = "qwertyuiop";
- String row2 = "asdfghjkl";
- String row3 = "zxcvbnm";
- String returnme = input + " can be written using the following rows of a keyboard: \n";
- int rowcounter = 1;
- int numbers = 0; int letters = 0; int symbols = 0;
- String linput = input.toLowerCase();
- for(int i = 0; i < linput.length(); i++)
- {
- if(row1.indexOf(linput.substring(i, i+1)) != -1)
- rowcounter *= 3;
- else if(row2.indexOf(linput.substring(i, i+1)) != -1)
- rowcounter *= 5;
- else if(row3.indexOf(linput.substring(i, i+1)) != -1)
- rowcounter *= 7;
- }
- if(rowcounter%3 == 0)
- returnme += "Row 1 \n";
- if(rowcounter%5 == 0)
- returnme += "Row 2 \n";
- if(rowcounter%7 == 0)
- returnme += "Row 3 \n";
- return returnme;
- }
- public static void shortestAndLongest()
- {
- String pattern = "([a-zA-z0-9,]+)|((([a-zA-z0-9,]+\\s)+)[a-zA-z0-9,]+)";
- String message1 = "Enter word/sentence/phrase";
- String message2 = "Which words are longest and shortest";
- String userInput = getWordOrPhraseFromEndUser(message1, message2, pattern);
- //boolean validInput = false;
- //String userInput = "";
- //String errorMessage = "Invalid Input";
- String[] words;
- String results = "";
- String longest = " ";
- String shortest = " ";
- int l = 0;
- int s = 0;
- /*while (!(validInput))
- {
- userInput = JOptionPane.showInputDialog(null, "Enter a word or sentence or phrase", "Which words are the longest and shortest", 3);
- userInput = userInput.replaceAll("\\s+"," ");
- userInput = userInput.trim();
- if (userInput == null || userInput.matches(pattern))
- validInput = true;
- else
- JOptionPane.showMessageDialog(null, errorMessage, "Error in user input", 2);
- }*/
- userInput = userInput.replaceAll(",","");
- words = userInput.split(" ");
- shortest = words[0];
- s = shortest.length();
- for (int i = 0; i < words.length; i++)
- {
- if (words[i].length()>longest.length())
- {
- longest = words[i];
- l = longest.length();
- }
- else if ((words[i].length()==longest.length()) && !(words[i].equalsIgnoreCase(longest)))
- longest = words[i] + " + " + longest;
- }
- for (int i = 1; i < words.length; i++)
- {
- if (words[i].length()<shortest.length())
- {
- shortest = words[i];
- s = shortest.length();
- }
- else if ((words[i].length()==shortest.length()) && !(words[i].equalsIgnoreCase(shortest)))
- shortest = words[i] + " + " + shortest;
- }
- results = "The maximum length is " + l + " and the word(s) matching this length are\\is \"" + longest + "\"\n";
- results += "The minimum length is " + s + " and the word(s) matching this length are\\is \"" + shortest + "\"";
- JOptionPane.showMessageDialog(null, results, "Results", 3);
- //System.out.print(longest + " " + shortest);
- }
- public static void areTheyAnagrams()
- {
- String phrase1, phrase2, results;
- String c;
- int j = 0;
- //char[] p1 = new char[phrase1.length()];
- //char[] p2 = new char[phrase2.length()];
- String message1 = "Are words/phrases anagrams?";
- String message2 = "Enter first word/phrase";
- String message3 = "Enter second word/phrase";
- String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
- phrase1 = getWordOrPhraseFromEndUser(message1, message2, pattern);
- if (phrase1 != null)
- {
- phrase2 = getWordOrPhraseFromEndUser(message1, message3, pattern);
- if (phrase2 != null)
- {
- phrase1 = phrase1.replaceAll(" ","");
- phrase2 = phrase2.replaceAll(" ","");
- phrase1 = phrase1.trim();
- phrase2 = phrase2.trim();
- if (phrase1.length() != phrase2.length())
- results = "They are not anagrams";
- else
- {
- while(j<phrase2.length())
- {
- c = phrase1.substring(0,1);
- if (c.equals(phrase2.substring(j,j+1)))
- {
- if (phrase2.length() != 1)
- {
- phrase1 = phrase1.substring(1);
- if (j != 0)
- phrase2 = phrase2.substring(0,j) + phrase2.substring(j+1,phrase2.length());
- else
- phrase2 = phrase2.substring(j+1,phrase2.length());
- j=0;
- }
- else
- {
- phrase2 = "";
- phrase1 = "";
- }
- }
- else
- j++;
- }
- }
- if (phrase1.length()==0)
- JOptionPane.showMessageDialog(null, "They are anagrams", "results", 3);
- else
- JOptionPane.showMessageDialog(null, "They are not anagrams", "results", 3);
- }
- }
- }
- public static void isItAPalindrome()
- {
- String input, temp, result = "";
- boolean palindrome1 = false, palindrome2 = false;
- String[] myarray;
- String message1 = "Is the word/phrase a palindrome?";
- String message2 = "Enter word/phrase";
- String pattern = "([a-zA-Z]+)|((([a-zA-Z]+\\s)+)[a-zA-Z]+)";
- input = getWordOrPhraseFromEndUser(message2, message1, pattern);
- input = input.toLowerCase();
- if (input != null)
- {
- if(input.indexOf(" ") == -1) //(WORD)
- {
- //Check for regular palindrome (WORD)
- myarray = input.split("");
- int i = 0, j = myarray.length - 1;
- do
- {
- //if(myarray[i].compareTo(myarray[j]) == 0)
- if(myarray[i].equals(myarray[j]))
- palindrome1 = true;
- else{
- palindrome1 = false;
- }
- i++; j--;
- }while( i < myarray.length && palindrome1);
- }
- else //(PHRASE)
- {
- //Check for word-unit palindrome (PHRASE)
- myarray = input.split("\\s");
- int i = 0, j = myarray.length - 1;
- do
- {
- if(myarray[i].equals(myarray[j]))
- palindrome2 = true;
- else
- palindrome2 = false;
- i++; j--;
- }
- while( i < myarray.length && palindrome2);
- //CHeck for regular palindrome(PHRASE)
- temp = input.replaceAll("[^a-z]","");
- myarray = temp.split("");
- i = 0; j = myarray.length - 1;
- do
- {
- if(myarray[i].equals(myarray[j]))
- palindrome1 = true;
- else
- palindrome1 = false;
- i++; j--;
- }
- while( i < myarray.length && palindrome1);
- }
- String firstLetter = input.substring(0,1).toUpperCase();
- input = firstLetter + input.substring(1);
- if(palindrome1 && palindrome2)
- result = input + " is a regular palindrome and also a palindrome at the word-unit level.";
- else if(palindrome1)
- result = input + " is a regular palindrome";
- else if(palindrome2)
- result = input + " is a palindrome at the word-unit level";
- else
- result = input + " is not a palindrome";
- JOptionPane.showMessageDialog(null, result);
- }
- }
- public static void analyzeCharacterContent()
- {
- //***
- String message1 = "Analyze character content";
- String message2 = "Enter word/phrase";
- String input = JOptionPane.showInputDialog(null, message2, message1, 3);
- //***
- //we don't need to differentiate between upper and lower case
- String lowerinput = input.toLowerCase();
- String result = "";
- //128 is not a valid symbol
- int[] ascii = new int[127];
- //takes the int value of the char at i and increments index i of ascii array
- for(int i=0; i < input.length(); i++)
- {
- int holder = (int) lowerinput.charAt(i);
- ascii[holder] = ascii[holder] + 1;
- }
- //checks ascii i isnt empty, then concats the char associated with ascii i
- //and the value at ascii i to result with some fluff.
- for(int i=33; i<127; i++)
- {
- if(ascii[i] != 0)
- {
- char concat = (char) i;
- int counter = ascii[i];
- result += concat + " appears " + counter + " times \n";
- }
- }
- //***
- JOptionPane.showMessageDialog(null, result);
- }
- public static String getWordOrPhraseFromEndUser(String windowMessage, String windowTitle, String pattern)
- {
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement