Guest User

Untitled

a guest
Jan 12th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. package dillavoun;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class Lab4 {
  6.  
  7. /**
  8. * @param args
  9. */
  10. public static void main(String[] args) {
  11. // TODO Auto-generated method stub
  12. do {
  13. String ans = JOptionPane.showInputDialog("Select from options:\n 1) Cats and Dogs \n 2) Mix Strings \n 3) Palindrome");
  14. switch(ans.charAt(0)){
  15. case '1':
  16. String catDog = JOptionPane.showInputDialog("Enter a phrase:");
  17. String catDogL = catDog.toLowerCase();
  18. int countCat = StringUtils.countMatches(catDogL, "cat");
  19. int countDog = StringUtils.countMatches(catDogL, "dog");
  20. JOptionPane.showMessageDialog (null, "Cats: "+ countCat + ", Dogs: "+ countDog);
  21. break;
  22. case '2':
  23. String wordA = JOptionPane.showInputDialog("Enter a word:");
  24. String wordB = JOptionPane.showInputDialog("Enter another word:");
  25. StringBuilder sb = new StringBuilder();
  26. for (int i = 0; i < wordA.length(); i++) {
  27. sb.append(wordA.charAt(i));
  28. sb.append(wordB.charAt(i));
  29. }
  30. String result = sb.toString();
  31. JOptionPane.showMessageDialog(null, result);
  32. break;
  33.  
  34. case '3':
  35. String palindrome = JOptionPane.showInputDialog("Enter a phrase:");
  36. String palindromeL = palindrome.toLowerCase();
  37. String palindromeLW = palindromeL.replaceAll(" ", "");
  38. int left = 0;
  39. int right = palindromeLW.length() - 1;
  40. while(left<right){
  41. if (palindromeLW.charAt(left) != palindromeLW.charAt(right)){
  42. JOptionPane.showMessageDialog(null, "You did not enter a palindrome.");
  43. }
  44. left ++;
  45. right --;
  46. }
  47. JOptionPane.showMessageDialog(null, "You did enter a palindrome.");
  48. break;
  49.  
  50. default:
  51. break;
  52. }while (ans != null);
  53.  
  54. }
  55. }
  56.  
  57. }
Add Comment
Please, Sign In to add comment