String comparison not working in android [duplicate] - android

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

Related

if-else block not working properly in android

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.

Check if is it empty on edit text is not working

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("")){

if statement is nt working in android code [duplicate]

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.

Standalone return expression explanation

I was online looking at some android coding examples and i came across a method that had a return expression by itself,and i was wondering if someone could explain what that means.
Here is the code sample:
if(tempText.getText().length() ==0){
Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show();
return;
}
This is the If condition which checks for the length of the text which is "0" or not. If its "0" then it will show the Toast message and will return from or exit from the if loop an no further execution will processed.
using if condition you are checking for the length of text, if it is 0 then you are showing Toast
Using a return Keyword within a class, with a method
A method returning nothing
public void Void_Method()
{
<statements>
return;
}
A method returning a String
public String String_Method()
{
String s = "its Ridiculous to do such kind of work, people here are just aim less";
return s;
}
A method returning an Int value
public int Int_Method()
{
int i = 5;
return(i);
}
I hope this will help you understand the use of return keyword, and as name suggest - The return keyword is always used with a method only to specify that the method is going to return something.

Build.Model doesn't provide the right information?

I am fairly new to the whole android-development.
I am trying to set a different layout depending on the Modelname.
I am working with the android-eclipse SDK
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final String model = Build.MODEL;
if(model == "sdk")
{
setContentView(R.layout.activity_test_one);
} else
{
setContentView(R.layout.activity_test_two);
}
Toast.makeText(getApplicationContext(), model, Toast.LENGTH_SHORT).show();
}
The Toast says that the modelname is "sdk", but the if statement isn't executed, as indeed the else-part is executed.
What could be the reason for that?
You cannot use the '==' operator on the String as it checks to see if the string object is the same, not its content. Use .equals(String) instead.
if ("sdk".equals(model) {
...
}

Categories

Resources