Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package space.indica.CheckPalindrome;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.lang.StringBuilder;
- public class CheckPalindrome {
- public static void main(String args[]) {
- FileReader input = null;
- BufferedReader reader = null;
- String word = null;
- String reverse = null;
- try {
- input = new FileReader("word.txt");
- } catch (FileNotFoundException e) {
- System.out.println("I couldnt find word.txt");
- }
- if (input != null) {
- System.out.println("File opened successfully!");
- reader = new BufferedReader(input);
- try {
- word = reader.readLine().toString();
- } catch (IOException e) {
- System.out.println("Could not read from file!");
- e.printStackTrace();
- }
- if (word != null) {
- System.out.println("The word is " + word + "!");
- } else {
- System.out.println("I couldnt get a word from the file.");
- }
- reverse = new StringBuilder(word).reverse().toString();
- if (word != null) {
- System.out.println("Reversed word is " + reverse);
- }
- if (reverse == word) {
- System.out.println("This word is a palindrome!");
- } else {
- System.out.println("This word is NOT a palindrome!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement