Comparing strings in Java
[EDIT]
I made a mistake earlier, because, to get the text, you need to use .getText().toString().
Here is a full working example:
package com.psegina.passwordTest01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener {
LinearLayout l;
EditText user;
EditText pwd;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
l = new LinearLayout(this);
user = new EditText(this);
pwd = new EditText(this);
btn = new Button(this);
l.addView(user);
l.addView(pwd);
l.addView(btn);
btn.setOnClickListener(this);
setContentView(l);
}
public void onClick(View v){
String u = user.getText().toString();
String p = pwd.getText().toString();
if( u.equals( p ) )
Toast.makeText(getApplicationContext(), "Matches", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), user.getText()+" != "+pwd.getText(), Toast.LENGTH_SHORT).show();
}
}
Original answer (Will not work because of the lack of toString())
Try using .getText() instead of .toString().
if( passw1.getText() == passw2.getText() )
#do something
.toString() returns a String representation of the whole object, meaning it won't return the text you entered in the field (see for yourself by adding a Toast which will show the output of .toString())
Android string compare is false
Use equals()
to compare strings for content, not ==
.
==
will check if the objects are the same.
String foo = "foo";
if (foo == foo) {
// same object, so true
}
String foo1 = "foo";
String foo2 = "foo";
if (foo1 == foo2) {
// both are string literals set at compile time, so true
}
String foo1 = loadFoo1(); // imagine this returns "foo"
String foo2 = loadFoo2(); // imagine this also returns "foo"
if (foo1 == foo2) {
// not the same object, and not string literals, so false
}
if (foo1.equals(foo2)) {
// both strings hold "foo", so true
}
Comparing two string in Java not giving the expected result
You have to use String.equals() method for string matching.
Change your code into this
if (editText.getText().toString().equals("admin")) {
//your code
}
.
String comparison not working in android
public void processFinish(String output) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), output,Toast.LENGTH_LONG).show();
String check="false";
if(output.trim().equals(check.trim())){
//doing something here
}else{
//something here
}
}
your string getting some unwanted blank space from method trim() remove starting and end blank space :) and .equals is a object class method which compare two string :)
Android Studio Java String Compare
It seems that String s has un-necessary characters present.
Try below:
if(s.contains("success"){}
Note: For string comparison, its always good practice to compare constant value with other string. This will save code from Null pointers.
if("success".equalsIgnoreCase(s)){}
Compare multiple strings android
If you have have a variable number of strings to check I would do something like the following:
private boolean validate(String... items) {
for (String item : items) {
if (TextUtils.isEmpty(item)) {
return false;
}
}
return true;
}
Then your code would call the validate method with as many strings as you need to validate:
if (validate(string1, string2, stringN)) {
Toast.makeText(this, "something is empty", Toast.LENGTH_SHORT).show();
} else {
// GO TO THE NEXT ACTIVITY
}
How to Compare two strings in android with optimization?
According to me, You should use the equals() method of the String class to compare Strings.
if (xyz.equals("something")) {
//Do what you want if string is equal...
}
see this also..
comparing two strings of EditTexts in android
Try it like this:
if(x.equalsIgnoreCase(y))
{
//----stuff--;
}
Follow this link to know more about it :
http://www.leepoint.net/data/expressions/22compareobjects.html
if statement comparing strings in java android
In java, when using ==
on two objects, you're not actually comparing the strings themselves. You'll need to use .equals(String)
.
==
actually compares the two object's references, not their values.
string1.equals(String target)
compares the two strings based off of the actual characters in the strings.
See: http://www.leepoint.net/notes-java/data/expressions/22compareobjects.html
Related Topics
Does Assigning Objects to Null in Java Impact Garbage Collection
Make Documentbuilder.Parse Ignore Dtd References
Java Thread Garbage Collected or Not
How Does a for Loop Work, Specifically For(;;)
Why Does String.Split Need Pipe Delimiter to Be Escaped
What Does Java Option -Xmx Stand For
Returning Null as an Int Permitted with Ternary Operator But Not If Statement
Convert Java.Util.Date to What "Java.Time" Type
Is It a Bad Practice to Catch Throwable
Java Executors: How to Be Notified, Without Blocking, When a Task Completes
Unresponsive Keylistener for Jframe
Convenient Way to Parse Incoming Multipart/Form-Data Parameters in a Servlet
How to Save the State of My Program and Then Load It
When Is the @JSONproperty Property Used and What Is It Used For