This question already has answers here:
How do I compare strings in Java?
(23 answers)
String.equals versus == [duplicate]
(20 answers)
Closed 9 years ago.
I'm having a slight problem that i can't understand, i'm building a Web Server that handles calls in the java E.G go to use /SendCommand.html then Java will handle the request, i have a login system built using post, but for some reason my login check is not working,
private boolean checkLogin(String user, String pass){
for(int i = 0; i < users.users.length; i++ ){
String test = SHA1.toSHA1(pass);
if(users.users[i][0] == user && users.users[i][1] == test ){
return true;
}
}
return false;
}
I'm Breaking at the if statment to provide the information below When i debug this i get,
Name | Type | Value
users Users #163
users String[] #165(length=1)
[0] String[] #167
[0] String "Admin"
[1] String "d033e22ae348aeb5660fc2140aec35850c4da997"
user String "Admin"
pass String "admin"
test String "d033e22ae348aeb5660fc2140aec35850c4da997"
As you can see users.users[0][0] == user and users.users[0][1] == test why is it returning false from the method?
Don't use == to compare strings. Use s1.equals(s2) instead. The former compares references, which is almost always not what you want. The latter, on the other hand, compares character sequences.
Use .equals() to compare strings, not ==.
if(users.users[i][0].equals(user) && users.users[i][1].equals(test))
Always compare String with .equals()
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am trying to check if a playlist is simple or master by an index value. My problem is when I put the (True) URL it still returns a false statement "This is a simple Playlist".
Any tips on how I can fix this ?
String output = getPlaylistUrl(input);
String mediaRecord = output.substring(399);
String lastRecord = "gear4/prog_index.m3u8";
if (mediaRecord == lastRecord) {
System.out.println("This is a master playlist");
} else {
System.out.println("This is a simple playlist");
}
In Java, strings can not be compared for equality using ==, because == compares two instances, not the content. So unless s1 and s2 are actually the same instance, s1 == s2 will never return true.
You need to use equals(...) to compare two strings for equality.
if (mediaRecord.equals(lastRecord) { ... }
In order to compare Strings you need to use .equals and not ==. Using == compares references and not values
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Hi I have a strange problem with my code and I cant figure out whats wrong.
I have:
ArrayList called players
Class: Player
Class: Name
Player class contains Name class which contains 3 Strings FirstName MiddleName LastName
The problem is when im trying to do
For( int i = 0; i < players.size(); i++)
{
if( players.get(i).getName().getFirst() == "First1")
{
// Some Code
}
System.out.printf(players.get(i).getName().getFirst());
}
If statement is never true, weird thing is when im using system.out to check as below
System.out.printf(players.get(i).getName().getFirst());
it returns:
First1 First2 First3
getName() method returns object name and getFirst() returns String FirstName
Any ideas where the problem is?
Never EVER EVER EVER compare Strings with ==. use .equals() instead.
"==" compares references of Strings. .equals() compares actual values.
To compare String objects in java use .equals() method instead of "==" operator. If you want ignore the case use .equalsIgnoreCase() method.
For String comparison you should use like this
if( "First1".equals(players.get(i).getName().getFirst()))
This question already has answers here:
How do I compare strings in Java?
(23 answers)
comparison of two Strings doesn't work in android [duplicate]
(4 answers)
Closed 9 years ago.
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 :)
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?
== 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.
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.
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.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
How do I know if String letter is equal to char array
String[] choose = {"a","d","t","b","s","f","x"};
String check;
boolean error = false;
System.out.print("Enter");
Scanner sn = new Scanner(System.in);
check = sn.nextLine();
for (int i = 0 ; i < choose.length;i++){
if(choose[i] == check){
System.out.print("you entered" + choose[i]);
break;
}
}
What I did is this it didnt confirm I input letter a but "you entered" didnt show up.
You cannot test strings for equality using ==. That only compares references (memory addresses). You need to use String#equals(Object). In general == is most certainly what you don't want if you are testing for equality, unless you are checking to see if two variables are pointing to the same instance. This is rarely the case, since you are usually interested in testing values for equality.
So what you need to do is:
if(choose[i].equals(check)) {
...
}
You are trying to compare strings with ==, which only compares the references, but not the values. What you want to use is
if(check.equals(choose[i]))
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
String is not equal to string?
What makes reference comparison (==) work for some strings in Java?
can some one explain me following java code
String a = "1";
if(a == "1") {
//print compare 1 is true;
} else {
//print compare 1 is false;
}
if(a.equals("1")) {
//print compare 2 is true;
} else {
//print compare 2 is false;
}
it results like
compare 1 is false
compare 2 is true
Only explanation i have is that its comparing the memory address not the values itself. But i am not sure. can some please put a light on it. in .Net == operator is overloaded to compare contents of string.
use "1".equals(a) , String is an object so use equals() to compare
I understood that == operator is compare "Is it same object?"
object a is not same object with constant string "1".
so returns false