• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to compare two StringBuffer or StringBuilder objects ?

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell me. Do i have to compare char by char or is there a simpler way ?
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and there is no need to write in UPPER-CASE, thank you very much.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I checked the api j6 and it says this :

public final class StringBuilder
extends Object
implements Serializable, CharSequence

A mutable sequence of characters. This...
reply
    Bookmark Topic Watch Topic
  • New Topic