> 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