Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Palindrome using StringBuffer

Reply
Thread Tools

Palindrome using StringBuffer

 
 
cat_dog_ass
Guest
Posts: n/a
 
      01-23-2007
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?

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      01-23-2007
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.

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      01-24-2007
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

 
Reply With Quote
 
abhi2varma abhi2varma is offline
Junior Member
Join Date: Jan 2013
Posts: 2
 
      01-05-2013
Quote:
Originally Posted by cat_dog_ass View Post
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
-------------------------------------------------------------------------------------------
 
Reply With Quote
 
abhi2varma abhi2varma is offline
Junior Member
Join Date: Jan 2013
Posts: 2
 
      01-05-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Re: Palindrome Tim Churches Python 2 06-05-2011 03:24 AM
Replacing palindrome substrings of an input string with a given string Tung Chau C Programming 1 08-06-2004 07:27 PM
Palindrome Runic911 Python 24 11-15-2003 12:08 AM
Re: Palindrome Pierre Quentel Python 2 11-13-2003 06:11 PM
Palindrome (HELP) Lorin Leone C++ 4 11-13-2003 08:11 AM



Advertisments