Solved

test two strings for equality

Posted on 2004-07-30
12
412 Views
Last Modified: 2010-03-31
what is the best way to see if two strings are exactly the same in java? its for when people enter a password into a form twice, i want to check to see that they entered the same password string both times.

thanks!
0
Comment
Question by:ew0kian
12 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 11682117
if (s1.equals(s2))
{
  // equal
}
0
 
LVL 3

Expert Comment

by:JavaInTheMorning
ID: 11682306
Since it's a password, the best thing is to use objects' suggestion.
But there is another possibility for other cases:
if (s1.equalsIgnoreCase(s2))
{
   // equal - case insensitive
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11683165
Use equals indeed and certainly not ==
Illustration:

        StringBuffer buf1 = new StringBuffer("Hello");
        StringBuffer buf2 = new StringBuffer("Hello");

        if (buf1.toString()==buf2.toString())
            System.out.println("1) equal");
        else
            System.out.println("1) not equal");  // <<<<<<< This is printed (wrong)

        if ( buf1.toString().equals(buf2.toString()) )

            System.out.println("2) equal");      // <<<<<<<< This is printed (correct)
        else
            System.out.println("2) not equal");
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11683217
Just to be complete: :-)

If you do this:

String pw1 = "Hallo".intern();
String pw2 = "Hallo".intern();

if ( pw1 == pw2 )
  System.out.println( "Equal" );
else
  System.out.println( "Not Equal" );

If you use intern() on both strings of a compare you can use == instead of equals().
But because intern() costs more than an equal it pays only if you do the compare very often.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11684066
Hi ew0kian,

If you're using a javax.swing JPasswordField to enter the passwords, you'll get a char[], not a string, which for security reason you can clear after use. If you want to compare 2 char[], use java.util.Arrays.equals(char_array1,char_array2).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:lowenbrausat
ID: 11686865
In case you are NOT using JPasswordField you can use the hashCode() method from String class.

String.hashCode() returns a hash code for the string. The hash code for a String object is computed as  
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)

Examples:

    int hash1 = "Hello".hashCode();
    int hash2 = "Hello".hashCode();
    System.out.println("Hash1 equals to Hash2? "+(hash1 == hash2)); //The result is TRUE

    int hash1 = "Hello".hashCode();
    int hash2 = "HellO".hashCode(); //Note the upper case 'O'
    System.out.println("Hash1 equals to Hash2? "+(hash1 == hash2)); //The result is FALSE


String.hashCode() is case-sensitive. This is the best way - and surest -  to do it.

Hope this can help you!!
c u arround! bye!!
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11686906
> String.hashCode() is case-sensitive. This is the best way - and surest -  to do it.

Why should this be better or surer than using equals() ???

0
 
LVL 92

Expert Comment

by:objects
ID: 11687024
> Why should this be better or surer than using equals() ???

its not
0
 
LVL 3

Expert Comment

by:JohnnyAffa
ID: 11687225
agrees with objects.......
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11687611
>> if (s1.equalsIgnoreCase(s2))

Since its a password, I'd prefer to do a case-sensitive comparision, rather than case-insensitive.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now