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

4
  • 3
    See the answer here stackoverflow.com/a/6819629/557179 and please use google to understand basic java concepts or use searchbar of stackoverflow :) Commented Jul 24, 2013 at 1:52
  • Interesting, first time I've seen a string comparison question on SO. How'd this get voted up? Commented Jul 24, 2013 at 1:59
  • Do some researching, put some effort in searching, .....these questions have already been asked stack-overflow before ! Commented Jul 24, 2013 at 2: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). Commented Jul 24, 2013 at 2:09

4 Answers 4

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?

Sign up to request clarification or add additional context in comments.

3 Comments

"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 Look into Java String Interning, that will answer your question
In the mean time, I indeed tried to find out the answer. And I found a very good explanation over here: 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...).
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.

Comments

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.

1 Comment

It works, it's just almost never what you actually want.
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.

6 Comments

This has nothing to do with being a scripting language or not.
Hey I was just proving why it was not part of the language features.
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.
As jahroy implied, the semantics of an operator has everything to do with how the language defines itself, not its "scriptiness".
@DaveNewton Whoops, Ill fix that right away.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.