This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mode.getText().toString()=="Default"){
Toast.makeText(getApplicationContext(), "Cannot Delete Default", Toast.LENGTH_SHORT).show();
}
//else{
/* db = openOrCreateDatabase("MY_App_Data", MODE_PRIVATE, null);
db.execSQL(DATABASE_TABLE_CREATE);
ContentValues cvInsert=new ContentValues();
cvInsert.put("mode",mode.getText().toString());
cvInsert.put("msg",msg.getText().toString());
db.insert("SSP_Data", null, cvInsert);
Toast.makeText(getApplicationContext(), "Mode Added", Toast.LENGTH_SHORT).show();
list.add(mode.getText().toString());*/
//}
}
});
hey guys in the above code my if statement is not working please help me out there iz no syntax error then why its nt executing please say smthing abt it
You can't compare Strings this way in Java. Use .equals()
if("Default".equals(mode.getText().toString()){
Using == checks if they are the same Object but not if they have the same values.
Also, putting the String in front of the variable this way protects against a NPE. Your mode.getText().toString() could return null but your literal String will not.
Use .equals or .equalsIgnoreCase to compare strings
if(mode.getText().toString().equals("Default")){
You have to use equals to compare strings, like this:
if(mode.getText().toString().equals("Default")){
Toast.makeText(getApplicationContext(), "Cannot Delete Default", Toast.LENGTH_SHORT).show();
}
You didn't say how it didn't work, but you almost certainly wanted this:
if (mode.getText().toString().equals("Default"))
rather than this:
if (mode.getText().toString() == "Default")
The first tests to see whether the two strings have the same content. The second tests to see if they're the same string.
Related
This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I have created an app in android studio and it uses a background service which has a method which starts a counter of seconds. I have created an if statement to test if the seconds are at 0 and when i debug the value seems to be correct but the condition is jumping straight to the else statement and i can not figure out why.
public void checkService(){
long secs = seconds;
String str = String.valueOf(secs);
if(str == "0")
{
Toast.makeText(this, "Not started", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Running", Toast.LENGTH_SHORT).show();
}
}
It is jumping to the second esle even when the value is 0. The image shows the value of str when i was debugging.
You should never test objects with ==. You should use .equals instead:
if(str.equals("0"))
{
Toast.makeText(this, "Not started", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Running", Toast.LENGTH_SHORT).show();
}
Some explanation at an answer from another post: https://stackoverflow.com/a/513839/2174489
I'm trying to check the editText condition. In the code below, I declared a setOnClickListener method to check the condition of editText. If condition is true, I want to print toast message, change the activity and to output a sound. If condition fails, it should toast a single message. In both cases if it's true or not, it prints me only "Incorect" no matter if editText is correct.
What I am doing wrong?
public void next(View v){
final MediaPlayer correctSound = MediaPlayer.create(this, R.raw.correctsound);
Button playCorrectSound = (Button) this.findViewById(R.id.angry_btn1);
final EditText editTextt = (EditText) findViewById(R.id.editText);
playCorrectSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editTextt.setVisibility(View.INVISIBLE);
if(editTextt.getText().toString() == "string")
{
Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_SHORT).show();
correctSound.start();
Intent i = new Intent(Hereuu.this, MainActivity.class);
startActivity(i);
} else {
Context context = getApplicationContext();
CharSequence text = "Incorect";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
editTextt.setVisibility(View.VISIBLE);
}
});
}
Like everyone had said, you
Basically, when you use == operator, your are checking if the reference for object are the same/equals. When you use .equals(String), the method checks the content.
tip:
When your are working with Strings, remember to avoid NullPointerException situations. So,you should write "constant".equals(editTextValue) instead of editTextValue.equals("constant")
The link bellow will help you to understand how String objects and String content work:
Java String.equals versus ==
regards
in java you need to compare strings using equals method instead of ==
check this topic for more details
I would suggest you to take some basic JAVA lessons. That will immensely help you.
For now, the problem is in the way you are checking equality of the strings. You do not use == with strings, you use String#equals() method. So,
Change
editTextt.getText().toString() == "string"
to
editTextt.getText().toString().equals("string")
Make sure to compare strings in java with .equals and not ==. Use this if statement:
if(editTextt.getText().toString().equals("string"){
Scenario: when the focus is lost from an EditText, I'm checking if it contains null (in the first if block).
If so, then I'll show a Toast.
In the else-if block I'm checking if the EditText doesn't contain letters.
Then I'll show a toast, but when I run the application, the Toast is shown even on a correct input.
I.e.: If I enter any letter the Toast should not be shown, it should be shown only when a null or digit/special symbol is entered.
Here is the code
et1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus)
{
a = et1.getText().toString();
if (a.equals(""))
{
Toast.makeText(getApplicationContext(), "Your entry is incorrect!!", Toast.LENGTH_LONG).show();
}
else if (!a.contains("[a-z]")||!a.contains("[A-Z]")) {
Toast.makeText(getApplicationContext(), "Your entry is incorrect!!", Toast.LENGTH_LONG).show();
}
else
{
}
Please help
The '==' operator only compares references. To compare string values you must use the equals() method.
Instead of
if (a == "")
use
if (a.equals(""))
See: What is the difference between == vs equals() in Java?
It's not working because:
if (a == "")
won't work in Java
Use
if (a.equals(""))
instead
Also, String.contains doesn't use regular expressions, but CharacterSequences.
So, unless your string doesn't contain the exact character sequences "[a-z]" or "[A-Z]" (and only one of these 2 strings), you'll never get a match.
See: http://developer.android.com/reference/java/lang/String.html#contains(java.lang.CharSequence)
The problem is:
if (a == "")
Strings can't be compared like this. Instead, check for size equal to 0, or against a specific string with the equals() method.
What i have is android app , and in one of activities i have two edit text with button, and i want either if one of them were empty when i click the button to preform a toast to tell the user to enter data, and if it was not empty i want it to open intent and do other thing , here is my code :
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(quantity.getText()==null){
Toast.makeText(FullImageActivity2.this,"please enter quantity",Toast.LENGTH_LONG).show();
}
else if(extra.getText()==null){
Toast.makeText(FullImageActivity2.this,"please enter extra",Toast.LENGTH_LONG).show();
}
else{
Quan=quantity.getText().toString();
name=itemId;
image=R.drawable.products;
Intent cart=new Intent(FullImageActivity2.this,CartList.class);
cart.putExtra("name", name);
cart.putExtra("quantity",Quan);
cart.putExtra("image", image);
Log.e("quan",Quan+"");
Log.e("name",name+"");
startActivity(cart);
}
}
});
But the weird thing that if they were empty , else is working !! which is not logic at all .. the validation on empty text is not empty , Why this is happening?? Help plz
Try to used .equals() method
if(quantity.getText().toString().trim().equals(""))
Try
if(editText.getText().toString().length()<1){
//do something
}
And of course you are mistaking null with "" which means no string but not null.
When Edittext is empty, getText() method returns empty string, not null.
Try:
if(quantity.getText().toString().equals("")){
Of course, if you want to avoid entering only spaces, use
if(quantity.getText().toString().trim().equals("")){
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I am trying to compare two string in my application but it fails to compare. I don't know why it fails.
public void processFinish(String output) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), output,Toast.LENGTH_LONG).show();
String check="false";
if(output==check){
//doing something here
}else{
//something here
}
}
the string object output has the value "false" but always the else block is executed why?
I Tried by changing the output=="false" to
output.equals(check)
output.equalsIgnoreCase(check)
output.contentEquals(check)
nothings works...
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 :)
try the following
if(output.contains(check)== true{
//doing something here
}else{
//something here
}
Since instances of String are object, so you can't comparison two objects with ==. To comparison two String, you have to use equals() or equalIgnoreCase() method.
Replace this
if(output==check){
//doing something here
}else{
//something here
}
with...
if(output.equals(check)){
//doing something here
}else{
//something here
}
I think output.equals(check) must works
Or your description maybe not correct.May it is not a string of "false" but "false "?
First ,check their length,then use output.trim().equals(check).