Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Palindrome using StringBuffer

Thread view: 
cat_dog_a.s - 23 Jan 2007 06:27 GMT
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?
Andrew Thompson - 23 Jan 2007 06:49 GMT
> 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.
Lew - 24 Jan 2007 00:50 GMT
> <sscce>
> public class Palindrome
[quoted text clipped - 8 lines]
> (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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.