Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dillavoun;
- import javax.swing.JOptionPane;
- public class Lab4 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- do {
- String ans = JOptionPane.showInputDialog("Select from options:\n 1) Cats and Dogs \n 2) Mix Strings \n 3) Palindrome");
- switch(ans.charAt(0)){
- case '1':
- String catDog = JOptionPane.showInputDialog("Enter a phrase:");
- String catDogL = catDog.toLowerCase();
- int countCat = StringUtils.countMatches(catDogL, "cat");
- int countDog = StringUtils.countMatches(catDogL, "dog");
- JOptionPane.showMessageDialog (null, "Cats: "+ countCat + ", Dogs: "+ countDog);
- break;
- case '2':
- String wordA = JOptionPane.showInputDialog("Enter a word:");
- String wordB = JOptionPane.showInputDialog("Enter another word:");
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < wordA.length(); i++) {
- sb.append(wordA.charAt(i));
- sb.append(wordB.charAt(i));
- }
- String result = sb.toString();
- JOptionPane.showMessageDialog(null, result);
- break;
- case '3':
- String palindrome = JOptionPane.showInputDialog("Enter a phrase:");
- String palindromeL = palindrome.toLowerCase();
- String palindromeLW = palindromeL.replaceAll(" ", "");
- int left = 0;
- int right = palindromeLW.length() - 1;
- while(left<right){
- if (palindromeLW.charAt(left) != palindromeLW.charAt(right)){
- JOptionPane.showMessageDialog(null, "You did not enter a palindrome.");
- }
- left ++;
- right --;
- }
- JOptionPane.showMessageDialog(null, "You did enter a palindrome.");
- break;
- default:
- break;
- }while (ans != null);
- }
- }
- }
Add Comment
Please, Sign In to add comment