1

Possible Duplicate:
How do I compare strings in Java?

(This may be a duplicate, I was not aware of .equals. My apologies.)

I was messing around in Java today when I decided to make a 4 character string generator. I have the program generate every possible combination of characters that I defined. This isn't for a project, I just wanted to see if this was possible. My problem lies with the string checking. I'll post the code first.

String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char[] chars = text.toCharArray();
String name = "Mike";
String pass;

outerLoop:
for (int a = 0; a < chars.length; a ++) {
    for (int b = 26; b < chars.length; b++) {
        for (int c = 26; c < chars.length; c++) {
            for (int d = 26; d < chars.length; d++) {
                pass = chars[a]+""+chars[b]+""+chars[c]+""+chars[d];
                System.out.println(pass);
                if (pass == name){
                    System.out.print("password");
                    break outerLoop;
                }
            }
        }
    }
}

The nested if will check if pass is equal to Mike. If it is, then it prints password and will break the for loop.

  • Is pass = chars[a]... the correct way to do this? When I tested it without the if, I had it print out pass and it printed all of the combinations correctly. It did print Mike, but it did not catch in the if.
  • I also changed the nested for loops so they start with the lower case because the program was taking a while to run when I made minor changes.
Community
  • 1
  • 1
Michael Garrison
  • 941
  • 2
  • 15
  • 31

3 Answers3

10
            if (pass == name){

should be

            if (pass.equals(name)){

use String.equals() method to check string equality. == operator simply checks if two reference variables refer to the same object. equals() method checks if two strings are meaningfully equal.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • 1
    Congratulations on 8888 rep :) – tckmn Dec 16 '12 at 22:06
  • I feel really silly for such a simple solution. My professor did not catch this either when I asked him about it before I posted. – Michael Garrison Dec 16 '12 at 22:17
  • @MichaelGarrison i don't think your professor would have not known it. he might have just left for you to figure it out so that you wouldn't do the same mistake later :) anyways, now you know how to check string equality.and also do read out about to objects are compared in java . :) – PermGenError Dec 16 '12 at 22:26
  • wow +1 for this. spent an hour trying to figure this out. Why does Java do this when most languages don't? I guess I expected cross-language compatibility. – kgui Jun 29 '17 at 18:31
0

Strings should be compared using equals()

AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

This comes up at least once per day. There should be a "close question" option dedicated to it. Nevertheless, here goes again...

The == operator tests if the two operands are the same instance.

The .equals() method compares the values of the two operands, but only if the class has implemented this method (which String does), otherwise it behaves the same as == (which is how the Object class implements it).

Bohemian
  • 412,405
  • 93
  • 575
  • 722