0

Possible Duplicate:
How do I compare strings in Java?

I check the internet and google about how to break in a while loop and checked it again but it seems not to break. and i lost hope tryin anymore

i run this code

  int i = 0;

    while( i <= 1236){

        System.out.println(i + " svarer til: "+md5(Integer.toString(i)));

        if(md5kryp == md5(Integer.toString(i))){


            System.out.println("Den dekryperet kode er: " + i);
            System.out.println("Den svare til den indtastede " +md5kryp);
            break;
        }

        i++;
    }

And i system print line it out to check if the encryption "i" match the encrypted md5. it runs it though. but doesn't break it.

i is at a point 81dc9bdb52d04dc20036dbd8313ed055(that is the number 1234) and the md5kryp is all the time 81dc9bdb52d04dc20036dbd8313ed055. so it does match but it runs though it and doesn't stop. so the break line doesn't work

Community
  • 1
  • 1
  • You should use `if(md5kryp.equals(md5(Integer.toString(i))))` instead `if(md5kryp == md5(Integer.toString(i)))` As they looks string to me – Smit Jan 17 '13 at 22:01

2 Answers2

3

md5kryp == md5(Integer.toString(i))

Use equals.

Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
1
md5kryp == md5(Integer.toString(i)

You can't compare Strings in Java like that. This comparison will compare, if this is the same object (guys, correct me if I am wrong).

What you really need as comparison is a method from the String-Class:

md5kryp.equals(md5(Integer.toString(i))
Stefan
  • 2,603
  • 2
  • 33
  • 62
  • `==` compares values. Object references are values. The reference stored in `md5kryp` is not the same as the one returned by `md5(Integer.toString(i))`, so they are not equal. `Object.equals()` compares using *some* equivalence relation, for `String`s this returns `true` when they represent the same sequence of characters. – Mattias Buelens Jan 17 '13 at 17:05