Palindrome using StringBuffer

C

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");
}
}
 
A

Andrew Thompson

cat_dog_ass said:
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");
}
}
}
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.
 
L

Lew

Andrew said:
<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
 
Joined
Jan 5, 2013
Messages
2
Reaction score
0
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
-------------------------------------------------------------------------------------------
 
Joined
Jan 5, 2013
Messages
2
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top