this question has answer here:
- comparing stringbuffer content equals 8 answers
the following code did not work. can tell me what's wrong following code. logically should work...
package assignments; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; public class ispalindrome { public static void main(string[] args) throws ioexception { bufferedreader br = new bufferedreader( new inputstreamreader(system.in)); system.out.println("enter word:"); stringbuffer sb1 = new stringbuffer(br.readline()); stringbuffer sb2 = new stringbuffer(sb1); sb1.reverse(); if(sb2.equals(sb1)) system.out.println("palindrome"); else system.out.println("not palindrome"); } }
try
sb1.tostring().equals(sb2.tostring());
because stringbuffer#tostring method returns string value of data stored inside buffer:
returns string representing data in sequence. new string object allocated , initialized contain character sequence represented object. string returned. subsequent changes sequence not affect contents of string.
Comments
Post a Comment