-1

I am working on a login page in an Android App.

As you know, the app must check if the username and password are valid, and then grant the user access to the application.

I have used the following code:

...

EditText un = (EditText) findViewById(R.id.username1);

EditText pw = (EditText) findViewById(R.id.password1);

String u = un.getText().toString();

String p = pw.getText().toString();


String myUser = "user1";

String myPass = "pass1";

//////// Now on the click of the Login Button:


public void onClickL (View view){

if ( (u == myUser) && (p == myPass)) /////// move to a new activity

else ///////Display a warning message: Try again

}

I entered the correct strings in both editText fields, however i always get the warning message. I don't understand what is wrong with it.

Please help :)

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
HudaOmais
  • 37
  • 1
  • 2
  • 4
  • 3
    See the answer here http://stackoverflow.com/a/6819629/557179 and please use google to understand basic java concepts or use searchbar of stackoverflow :) – Nikola Despotoski Jul 24 '13 at 01:52
  • Interesting, first time I've seen a string comparison question on SO. How'd this get voted up? – Dave Newton Jul 24 '13 at 01:59
  • Do some researching, put some effort in searching, .....these questions have already been asked stack-overflow before ! – Devrath Jul 24 '13 at 02:06
  • @DaveNewton - It gets voted up because people who answered it are (selfishly) hoping they'll get reputation. In my opinion, upvoting and/or answering questions like this hurts StackOverflow. The goal should be to link all duplicates to one authoritative question and answer (not to have dozens of identical questions, each one having dozens of similar answers). – jahroy Jul 24 '13 at 02:09

4 Answers4

11

You should use the equals() method of the String class to compare Strings. The == comparison only compares object references.

if (p.equals("Password")) { 
    //Do stuff 
}

So what you have should be changed to:

if ((u.equals(myUser)) && (p.equals(myPass))) {
    // do stuff
}

See here for a lot more information about this often-mixed-up topic: How do I compare strings in Java?

Community
  • 1
  • 1
Kon
  • 10,702
  • 6
  • 41
  • 58
  • "The == comparison only compares object references." That is also what I thought. However, why is the below code showing "Same" when both strings have the same content and "Different" when the content is not the same? Or is the Java compiler "clever" enough and it "sees" the content of both strings is the same, so it puts the same reference in both strings S1 and S2 and it occupies only one memory location on the heap to store the text "test1"? Puzzled. String S1 = "test1"; String S2 = "test1"; if (S1 == S2) { System.out.println("Same"); } else { System.out.println("Different"); } – GeertVc Feb 07 '16 at 11:02
  • @GeertVc Look into Java String Interning, that will answer your question – Kon Feb 07 '16 at 19:20
  • In the mean time, I indeed tried to find out the answer. And I found a very good explanation over here: http://stackoverflow.com/a/17942294/1252050. That, in fact, explains everything (especially the part about the "string constant pool", which I didn't know of and which gives the answer to my question...). – GeertVc Feb 09 '16 at 06:18
0

Always use String.equals(string) to compare strings. == will compare if the references are equal which doesn't work the way you want for strings.

Mark M
  • 1,580
  • 10
  • 22
0

== will do an object comparison between the strings in this situation, and although the value may be the same of the String objects, the objects are not the same. Hence why we use String.equals(string); to compare the value of two string objects. So if(u.equals(string)) and p.equal(string)are probably what you are looking for.

CBIII
  • 845
  • 1
  • 9
  • 9
0

Since java doesn't have a few modern features, == does not work on strings. Instead, it is a little more complicated.

To check if two string are equal, in the if statement put:

String.equals(otherString)

To compare lengths, use the .length method to compare them, and you could use ==.

Thanks.

verymessi
  • 117
  • 1
  • 12
  • 4
    This has nothing to do with being a scripting language or not. – Dave Newton Jul 24 '13 at 02:00
  • Hey I was just proving why it was not part of the language features. – verymessi Jul 24 '13 at 02:02
  • 1
    No you weren't. You've done nothing to prove anything. In fact, you've provided misleading information. There is no correlation between scripting languages and comparison operators. – jahroy Jul 24 '13 at 02:10
  • 1
    As jahroy implied, the semantics of an operator has everything to do with how the language defines itself, not its "scriptiness". – Dave Newton Jul 24 '13 at 02:18
  • @DaveNewton Whoops, Ill fix that right away. – verymessi Jul 24 '13 at 03:48
  • It has nothing to do with being "modern", either. It's entirely because == checks the value of the variable, whether it's a reference or an integer etc., period. – Dave Newton Jul 24 '13 at 09:37