How does equals method work on a String Buffer object!

Discussion in 'Java' started by javabeginner29, Apr 2, 2012.

  1. javabeginner29

    javabeginner29

    Joined:
    Apr 2, 2012
    Messages:
    1
    My program is:
    Code (Text):
    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?
     
    javabeginner29, Apr 2, 2012
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward A Thompson
    Replies:
    4
    Views:
    601
    Tony Morris
    Feb 11, 2004
  2. Matt
    Replies:
    5
    Views:
    16,600
    Roedy Green
    Jul 4, 2004
  3. Matt
    Replies:
    24
    Views:
    3,589
    Tony Morris
    Jul 8, 2004
  4. minjie@excite.com
    Replies:
    28
    Views:
    14,297
    nospam_googlegroups@scovetta.com
    Mar 21, 2006
  5. kvnsmnsn@hotmail.com
    Replies:
    5
    Views:
    1,364
    Tony Morris
    Apr 26, 2006
Loading...

Share This Page