The moose likes Beginning Java and the fly likes How to compare two StringBuffer or StringBuilder objects ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to compare two StringBuffer or StringBuilder objects ? " Watch "How to compare two StringBuffer or StringBuilder objects ? " New topic
Author

How to compare two StringBuffer or StringBuilder objects ?

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Please tell me. Do i have to compare char by char or is there a simpler way ?

SCJP 6. Learning more now.
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
package com.tcs.test;
public class BufferExample {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer();
buffer.append("test1");
buffer.append("test2");
buffer.append("test3");
StringBuffer buffer1 = new StringBuffer();
buffer1.append("test1");
buffer1.append("test2");
buffer1.append("test3");
System.out.println(buffer.toString().equals(buffer1.toString()));
}
}
output: true

This is what you looking for ?
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

dileep keely wrote:package com.tcs.test;
public .....
...output: true

This is what you looking for ?


Can we do it without converting it to string ? Is it ok to define equals() method for this ?


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
You can try overriding the equals method; StringBuilder has an unoverridden equals() method because its contents are intended to change rapidly.

By the way: why are you using StringBuffer rather than StringBuilder?
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I am trying to make the below program work PROPERLY (it compiles) : I read in the K&B book that password entered via readPassword() of console must not be saved as a string because it may remain in the string pool and be misused...blah...blah...hence i am avoiding the use of string in the below question.

The Aim :
Password class takes name and password using Console class. It calls a User object's method with these as arguments.
The User class compares these args with its variables and lets us know the result of the checking - name is ok/not , pass is ok/not.

THE PROBLEM :
I enter the correct user name and and pass , and still get wrong user name message

Please help me out with this, BTW i use string builder now and not buffer.



PS : i am having trouble in indenting code here, some of it appears at the right place in editor, but incorrectly after posting. please advise. (edited by me)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16690
    
  19

Rahul Sudip Bose wrote:
PS : I AM HAVING TROUBLE IN INDENTING CODE HERE, SOME OF IT APPEARS AT THE RIGHT PLACE IN EDITOR, BUT INCORRECTLY AFTER POSTING. PLEASE ADVISE.


This is likely caused by the mixing of spaces and tabs. Basically, your tab markers may not be the same as others, so don't mix if possible.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
. . . and there is no need to write in UPPER-CASE, thank you very much.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Campbell Ritchie wrote: . . . and there is no need to write in UPPER-CASE, thank you very much.


not shouting as the tut taught ... no offence was intended. Will do it properly in the future.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2782
    
    2
Rahul Sudip Bose wrote:I am trying to make the below program work PROPERLY (it compiles) : I read in the K&B book that password entered via readPassword() of console must not be saved as a string because it may remain in the string pool and be misused...blah...blah...hence i am avoiding the use of string in the below question.

Hmmm, this doesn't make any sense to me. The only ways strings get in the string pool are if either (a) they appear as string literals somewhere in one of your classes, or (b) you call the intern() method on a string. You're not doing either of these things, right? Then the string pool is irrelevant. If you want to compare these objects, using toString() seems a perfectly good way to do it.

Even if you were putting passwords in the String pool, it's not clear why this would be a problem. I can't think of any way to exploit this unless you're able to hack the JVM itself - and if you can do that, running your own malicious code in place of the JVM, then the passwords will be vulnerable no matter what you do. Has anyone here heard of a security exploit based on the string pool? Got any links?

Anyway, the equals() method of StringBuilder is useless. If you want to compare contents, you basically need to either use toString() first, or you need to look through and compare characters one by one using charAt(). Which is also a perfectly good way to do this.

Campbell Ritchie wrote:You can try overriding the equals method; StringBuilder has an unoverridden equals() method because its contents are intended to change rapidly.

Can't - StringBuilder and StringBuffer are both final.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
Rahul Sudip Bose wrote: . . . not shouting as the tut taught ... no offence was intended. Will do it properly in the future.
Apology accepted
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
Mike Simmons wrote: . . . Can't - StringBuilder and StringBuffer are both final.
I looked on the API documentation pages and didn't see final. I have obviously looked in the wrong place, because it says final elsewhere.

Thank you.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I checked the api j6 and it says this :

public final class StringBuilder
extends Object
implements Serializable, CharSequence

A mutable sequence of characters. This...
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to compare two StringBuffer or StringBuilder objects ?
 
Similar Threads
Array Project Help
problem in finding character
ArrayList Problem
String to char problems
K&B master exam question