Advertisement
popatop15

Untitled

Aug 8th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package space.indica.CheckPalindrome;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.lang.StringBuilder;
  8.  
  9.  
  10. public class CheckPalindrome {
  11.  
  12. public static void main(String args[]) {
  13.  
  14. FileReader input = null;
  15. BufferedReader reader = null;
  16. String word = null;
  17. String reverse = null;
  18.  
  19. try {
  20.  
  21. input = new FileReader("word.txt");
  22.  
  23. } catch (FileNotFoundException e) {
  24.  
  25. System.out.println("I couldnt find word.txt");
  26.  
  27. }
  28.  
  29. if (input != null) {
  30.  
  31. System.out.println("File opened successfully!");
  32.  
  33. reader = new BufferedReader(input);
  34.  
  35. try {
  36.  
  37. word = reader.readLine().toString();
  38.  
  39. } catch (IOException e) {
  40.  
  41. System.out.println("Could not read from file!");
  42. e.printStackTrace();
  43.  
  44. }
  45.  
  46. if (word != null) {
  47.  
  48. System.out.println("The word is " + word + "!");
  49.  
  50. } else {
  51.  
  52. System.out.println("I couldnt get a word from the file.");
  53.  
  54. }
  55.  
  56. reverse = new StringBuilder(word).reverse().toString();
  57.  
  58. if (word != null) {
  59.  
  60. System.out.println("Reversed word is " + reverse);
  61.  
  62. }
  63. if (reverse == word) {
  64.  
  65. System.out.println("This word is a palindrome!");
  66.  
  67. } else {
  68.  
  69. System.out.println("This word is NOT a palindrome!");
  70.  
  71. }
  72.  
  73. }
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement