Palindrome using StringBuffer

Discussion in 'Java' started by cat_dog_ass, Jan 23, 2007.

  1. cat_dog_ass

    cat_dog_ass Guest

    This is my program:
    -----------------------------------------------------------------------------
    public class Palindrome
    {
    public static void main(String[] args)
    {
    String str1= new String("malayalam");
    StringBuffer str2= new StringBuffer(str1);
    StringBuffer str3= new StringBuffer(str1);
    str2.reverse();

    System.out.println("First String:"+str2+ " Length:"+str2.length());
    System.out.println("Second String:"+str3+ " Length:"+str3.length());

    if (str2.equals(str3))
    System.out.println("Was a palindrome");
    else
    System.out.println("Was not a palindrome");
    }
    }
    -------------------------------------------------------------------------
    The output I'm getting is:
    ---------------------------------------------------------------------------
    First String:malayalam Length:9
    Second String:malayalam Length:9
    Was not a palindrome
    ----------------------------------------------------------------------------
    Where is the problem?
    cat_dog_ass, Jan 23, 2007
    #1
    1. Advertising

  2. cat_dog_ass wrote:
    > This is my program:

    (snip!) This is my version of your code..

    <sscce>
    public class Palindrome
    {
    public static void main(String[] args)
    {
    //String str1= new String("malayalam");
    String str1= new String("malayalay");
    StringBuffer str2= new StringBuffer(str1);
    StringBuffer str3= new StringBuffer(str1);

    str2.reverse();

    System.out.println("First String: \t" +
    str2 + " Length:" + str2.length());
    System.out.println("Second String: \t" +
    str3 + " Length:" + str3.length());


    // compare the contents of the StringBuffers,
    // rather than the references to the SB's Objects.
    if (str2.toString().equals(str3.toString())) {
    // add brackets to if/else for clarity
    // even if only using a single statement
    System.out.println("Was a palindrome");
    } else {
    System.out.println("Was not a palindrome");
    }
    }
    }
    </sscce>

    > Where is the problem?


    See comments in the code.

    As an aside - when posting code, please do *not*
    remove all indentation, but instead replace tab
    indents for 2-3 space characters.

    HTH

    Andrew T.
    Andrew Thompson, Jan 23, 2007
    #2
    1. Advertising

  3. cat_dog_ass

    Lew Guest

    Andrew Thompson wrote:
    > <sscce>
    > public class Palindrome
    > {
    > public static void main(String[] args)
    > {
    > //String str1= new String("malayalam");
    > String str1= new String("malayalay");
    > StringBuffer str2= new StringBuffer(str1);
    > StringBuffer str3= new StringBuffer(str1);
    >
    > (snip!)
    > </sscce>


    Also, for non-thread-safe use there is StringBuilder now instead of
    StringBuffer. Sun claims, "The StringBuilder class should generally be used in
    preference to [StringBuffer], as it supports all of the same operations but it
    is faster, as it performs no synchronization."

    Sorta like Vector / ArrayList.

    - Lew
    Lew, Jan 24, 2007
    #3
  4. cat_dog_ass

    abhi2varma

    Joined:
    Jan 5, 2013
    Messages:
    2
    Dude just replace your
    if (str2.equals(str3)) to
    if (str2.toString().equals(str3.toString()))
    your code will be this..
    ----------------------------------------------------------------------------
    package Hello;

    public class Palindrome
    {
    public static void main(String[] args)
    {
    String str1= new String("malayalam");
    StringBuffer str2= new StringBuffer(str1);
    StringBuffer str3= new StringBuffer(str1);
    str2.reverse();

    System.out.println("First String:"+str2+ " Length:"+str2.length());
    System.out.println("Second String:"+str3+ " Length:"+str3.length());

    if (str2.toString().equals(str3.toString()))
    System.out.println("Was a palindrome");
    else
    System.out.println("Was not a palindrome");
    }
    }
    For further query dont hesitate to ask me..
    m on gmail my email id is abhi2varma
    -------------------------------------------------------------------------------------------
    abhi2varma, Jan 5, 2013
    #4
  5. cat_dog_ass

    abhi2varma

    Joined:
    Jan 5, 2013
    Messages:
    2
    Originally Posted by cat_dog_ass
    This is my program:
    -----------------------------------------------------------------------------
    public class Palindrome
    {
    public static void main(String[] args)
    {
    String str1= new String("malayalam");
    StringBuffer str2= new StringBuffer(str1);
    StringBuffer str3= new StringBuffer(str1);
    str2.reverse();

    System.out.println("First String:"+str2+ " Length:"+str2.length());
    System.out.println("Second String:"+str3+ " Length:"+str3.length());

    if (str2.equals(str3))
    System.out.println("Was a palindrome");
    else
    System.out.println("Was not a palindrome");
    }
    }
    -------------------------------------------------------------------------
    The output I'm getting is:
    ---------------------------------------------------------------------------
    First String:malayalam Length:9
    Second String:malayalam Length:9
    Was not a palindrome
    ----------------------------------------------------------------------------
    Where is the problem?
    Dude just replace your
    if (str2.equals(str3)) to
    if (str2.toString().equals(str3.toString()))
    your code will be this..
    ----------------------------------------------------------------------------
    package Hello;

    public class Palindrome
    {
    public static void main(String[] args)
    {
    String str1= new String("malayalam");
    StringBuffer str2= new StringBuffer(str1);
    StringBuffer str3= new StringBuffer(str1);
    str2.reverse();

    System.out.println("First String:"+str2+ " Length:"+str2.length());
    System.out.println("Second String:"+str3+ " Length:"+str3.length());

    if (str2.toString().equals(str3.toString()))
    System.out.println("Was a palindrome");
    else
    System.out.println("Was not a palindrome");
    }
    }
    For further query dont hesitate to ask me..
    m on gmail my email id is abhi2varma
    abhi2varma, Jan 5, 2013
    #5
    1. Advertising

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. Lorin Leone

    Palindrome (HELP)

    Lorin Leone, Nov 12, 2003, in forum: C++
    Replies:
    4
    Views:
    1,005
    Chris Theis
    Nov 13, 2003
  2. Runic911

    Palindrome

    Runic911, Nov 13, 2003, in forum: Python
    Replies:
    24
    Views:
    1,588
    Andrew Dalke
    Nov 15, 2003
  3. Tim Churches

    Re: Re: Palindrome

    Tim Churches, Nov 13, 2003, in forum: Python
    Replies:
    2
    Views:
    737
    yousafzai
    Jun 5, 2011
  4. Pierre Quentel

    Re: Palindrome

    Pierre Quentel, Nov 13, 2003, in forum: Python
    Replies:
    2
    Views:
    508
    Francis Avila
    Nov 13, 2003
  5. Tung Chau
    Replies:
    1
    Views:
    468
    SM Ryan
    Aug 6, 2004
Loading...

Share This Page