Guest User

Untitled

a guest
Feb 28th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. /*
  2. * @author Mines_of_Moria
  3. * Sunday, February 2nd, 2014
  4. */
  5.  
  6. package palindrome;
  7.  
  8. import java.util.Scanner;
  9.  
  10. public class Palindrome {
  11.  
  12.  
  13.  
  14. public static void main(String[] args) {
  15.  
  16. String userString;
  17. Scanner kb = new Scanner(System.in);
  18.  
  19. System.out.println("This program will attempt to tell you if your String is a palindrome" +
  20. " just as it is, a first class palindrome, or a second class palindrome.\n");
  21. System.out.printf("Please enter a String: ");
  22. userString = kb.nextLine();
  23.  
  24. System.out.println("\nYou entered: " + userString + "\n");
  25.  
  26. System.out.println("Method A uses String and Character methods to determine if your String" +
  27. " is a palindrome just as it is.\n");
  28.  
  29. if (methodA(userString)) {
  30. System.out.println("Method A result: " + userString + " is a palindrome just as it is.\n");
  31. } else {
  32. System.out.println("Method A result: " + userString + " is NOT a palindrome just as it is.\n");
  33. }
  34.  
  35. System.out.println("Method B uses the StringBuilder reverse method to tell us if a String" +
  36. " is a palindrome just as it is, without changing case or ignoring non-letter characters. \n" +
  37. "This is a first class palindrome.\n");
  38.  
  39. if (methodB(userString)) {
  40. System.out.println("Method B result: " + userString + " is a first class palindrome.\n");
  41. } else {
  42. System.out.println("Method B result: " + userString + " is NOT a first class palindrome.\n");
  43. System.out.println("Method C tells us if the letters in a String form a palindrome if we " +
  44. "ignore case, white spaces, and punctuation. This is a second class palindrome.\n");
  45.  
  46. if (methodC(userString)) {
  47. System.out.println("Method C result: " + userString + " is a second class palindrome.\n");
  48. } else {
  49. System.out.println("Method C result: " + userString + " is NOT a second class palindrome.\n");
  50. }
  51. }
  52. } // end main
  53.  
  54. // The following checks the first and last characters are the same, then walks
  55. // further inside the string checking the results each time. It returns as soon
  56. // as the String isn't a palindrome.
  57.  
  58. static boolean methodA(String userString) {
  59. int n = userString.length();
  60. for (int i=0;i<(n/2);i++) {
  61. if (userString.charAt(i) != userString.charAt(n - i - 1)) {
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67.  
  68. // The following returns if StringBuilder(user entered String) equals the
  69. // reverse of StringBuilder
  70.  
  71. static boolean methodB(String userString) {
  72. String reversed = new StringBuilder(userString).reverse().toString();
  73. return userString.equals( reversed );
  74. }
  75.  
  76. // The following writes a regular expression to match blanks and punctuation,
  77. // and provides and empty string ("") as the replacement. This returns
  78. // the original minus the characters that need ignored.
  79.  
  80. static boolean methodC(String userString) {
  81. userString = userString.toLowerCase().replaceAll("\\W", "");
  82. return (methodB(userString));
  83. }
  84.  
  85. /* This method reads data from the file into the array.
  86. * This array works with up to 100 elements.
  87. * Each line from the file will be one element in the array.
  88. * The parameter refers to the array in the main method.
  89. * The method returns the number of elements it uses.
  90. */
  91.  
  92. } // end class Palindrome
Advertisement
Add Comment
Please, Sign In to add comment