- Joined
- Apr 2, 2012
- Messages
- 1
- Reaction score
- 0
My program is:
This program checks if a string is a palindrome by reading each word which is on a different line from the file Word.txt. This works .. But my question is :
when I give if(strLine.equals(strLine.reverse()) it doesnot give the desired output. It simply prints all the words in the file Word.txt concatenated with " is a palindrome". Why is it so?
Code:
import java.io.*;
class Palindrome
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Words.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while(br.readLine()!= null){
StringBuffer strLine = new StringBuffer(br.readLine());
String str1 = strLine.toString();
String str2 = strLine.reverse().toString();
if (str1.equals(str2)) {
System.out.println(str1 + " is a palindrome");
}
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
This program checks if a string is a palindrome by reading each word which is on a different line from the file Word.txt. This works .. But my question is :
when I give if(strLine.equals(strLine.reverse()) it doesnot give the desired output. It simply prints all the words in the file Word.txt concatenated with " is a palindrome". Why is it so?