Solved

test two strings for equality

Posted on 2004-07-30
12
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

695 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