Solved

test two strings for equality

Posted on 2004-07-30
12
402 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
Comment Utility
if (s1.equals(s2))
{
  // equal
}
0
 
LVL 3

Expert Comment

by:JavaInTheMorning
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Expert Comment

by:lowenbrausat
Comment Utility
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
Comment Utility
> 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
Comment Utility
> Why should this be better or surer than using equals() ???

its not
0
 
LVL 3

Expert Comment

by:JohnnyAffa
Comment Utility
agrees with objects.......
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> if (s1.equalsIgnoreCase(s2))

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

772 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

8 Experts available now in Live!

Get 1:1 Help Now