Incompatible types in string - android

Resources r1 = getResources();
String[] refrigerant = r1.getStringArray(R.array.refrigerant);
if (refrigerant == "")
{
if (et1.getText().toString() == refrigerant[i3]
{
flag = true;
}
I got the error incompatible operand types String[] and string
please give me solution.

refrigerant is an array, but here refrigerant == "" you compare and array with "", which is not possible. You could check for null and refrigerant.length >0

If refrigerant is an String array you can not compare it as an empty String.
Try
if(refrigerant == null || refrigerant.length == 0){
}

Note also that comparing Strings using == as in your second if clause will very often not work, as it tests object identity. You usually want to use string1.equals(string2).

looks like you are trying to compare a string array to a string in your first "if" statement

Related

Android string compare is false [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I have the following piece of code:
String week = this.getIntent().getStringExtra("weekNumber");
String correctWeek = Integer.toString(Calendar.WEEK_OF_YEAR);
if (week == correctWeek) {
correct();
}
else incorrect();
They are both "3", but the comparation result is false and I don't know why:
Where is the error?
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
}
The two strings are separate objects, and "==" tests if its two operands are the same exact object.
To compare strings, try week.equals(correctWeek)
The "==" operator is used for reference comparison. It checks if both objects point to the same memory location, which returns false in your case as you are comparing two different string objects.
For your purpose you should use .equals(), which evaluates to the comparison of values in the objects.
Example:
String week = new String("3");
String correctWeek = new String("3");
System.out.println(week == correctWeek);
System.out.println(week.equals(correctWeek));
will output:
false
true

multiple check or,and in if statement

hi i'm wonder why my if always Toast me : "names Successfully saved!"
i'm try every thing.
public void btnSave_Clicked(View view) {
TextView txtOname = (TextView)findViewById(R.id.txtOname);
TextView txtXname = (TextView)findViewById(R.id.txtXname);
String X = txtXname.getText().toString();
String O = txtOname.getText().toString();
if((X!="") && (O!="")){
DatabaseHelper.insertName(getBaseContext(),((TextView)findViewById(R.id.txtOname))
.getText().toString());
DatabaseHelper.insertName(getBaseContext(),((TextView)findViewById(R.id.txtXname))
.getText().toString());
Toast.makeText(this,"names Successfully saved!",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"E",Toast.LENGTH_SHORT).show();
}
}
}
Strings are reference types in Java, and thus the reference of a dynamically created empty string will be different from your variables. Another option to isEmpty is equals.
if (!x.equals("") && !o.equals(")) {
//code
}
Though I'd probably go with isEmpty
Replace your if statement with:
if (!x.isEmpty() && !o.isEmpty()) {
//code
}
operator == compares Object reference.
.equals() compares String value.
.isEmpty() return true if String length is 0.
Strings are objects. Object instances (the value behind them) have to be compared manually with a method to assure that the content is the same.
The == operator just compares the string references ("adresses"). So when you create 2 object instances at runtime, they have different adresses even if the content is the same. Compile-time strings on the other hand are internalized, they are put into special memory and duplicates are sorted out.
System.out.println(new String("test") == new String("test"));
This will print false, because those 2 objects get created at runtime. The new keyword in the first example mandates that a new object with a new adress is created.
System.out.println("test" == "test");
This will print true, because they are String literals, which are known at runtime, you are not explicitly stating the new keyword here either. You are simply specifying that you want those literals represented in the code somehow, so the compiler internalizes them.

Can anyone suggest me that below if condition is wrong or not? [duplicate]

This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 6 years ago.
May occur any exception in coding for checking string is null or not ? Please help.
String code ;
if (!code.equals(null)) {
}
else
{
}
Here is how you can check if String value is null or not
if(code != null){
}else{
}
You can not us !code.equals(null) because, equals is used to compare same object type. null is not any object type and code is String. If you consider null as String, then you can use !code.equals("null")
String can be checked like this:
if(code.equals("null") || code.equals("")) {
// code to do when string is null.
}
else {
// code to do when string is not null.
}
equals() is use to check the similarity of two String, and == or != is use to check the condition. In your case you are checking the similarity of string.
if (!code.equals(null)) {
//code checks that String code is equal to null or not
}
else
{
}
another
if (code != null) {
//code checks if code is not equals to null (condition checking)
}
else
{
}
There are many ways to check if String is empty in Java, but what is the right way of doing it? right in the sense of robustness, performance and readability. If robustness is your priority then using equals() method or Apache commons StringUtils is the right way to do this check. If you don't want to use third party library and happy of doing null check by yourself, then checking String's length is the fastest way and using isEmpty() method from String is most readable way. By the way, don't confuse between empty and null String, if your application treat them same, then you can consider them same otherwise they are different, as null may not be classified as empty. Here are three examples of checking String is empty or not by using JDK library itself.
Read more Here
You can't use .equals(null) to make a null check, because the API description for Object#equals states that:
For any non-null reference value x, x.equals(null) should return false.
Not only would this be a useless check (since it always returns false), it would also throw a NullPointerException if code actually was null, because a null value does not define an equals method.
Object x = null;
boolean isNull = x.equals(null); // NullPointerException on .equals
The only practical way to do a null check is to use:
if (code != null) {
}
If you want to check whether string is empty i.e. null or "" then use
if(TextUtils.isEmpty(code)){
}else{
}
equals checks the value exists in string.

"==" for Strings always returning false [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I can't seem to get the code within the second if statement to execute. I have logged both values being compared and also ran the debugger and checked them. They are both "a". It always shows the incorrectPasswordDialog. This question seems difficult because it seems as though it should just work but any help is appreciated.
private void logUserIn(AppUser user) {
if (user != null){
Log.d("mPassword: ", mPassword);
Log.d("user.getPassword(): ", user.getPassword());
String userPassword = user.getPassword().toString();
String formPassword = mPassword.toString();
if ( userPassword == formPassword ){
Intent welcomePage = new Intent(this, StartScreenActivity.class);
welcomePage.putExtra("name", mName);
startActivity(welcomePage);
}
else {
showIncorrectPasswordDialog();
}
}else {
showIncorrectUserNameDialog();
}
}
You are comparing the object identity. Use string.equals() to check for equivalence.
if(userPassword.equals(formPassword)){
}
Change
if ( userPassword == formPassword ){
to
if ( userPassword.equals(formPassword) ){
== compares object references, whereas .equals compared String values.
You can not compare string using ==
try like this
Compares the specified object to this string and returns true if they are equal. The object must be an instance of string with the same characters in the same order.
if ( userPassword.equals(formPassword)){
// do some stuff
}
or
Compares the specified string to this string ignoring the case of the characters and returns true if they are equal.
if(userPassword.equalsIgnoreCase(formPassword))
{
//do some stuff
}
In JAVA to compare between strings, you should use:
if(userPassword.equals(formPassword)) {
// they are equal
}
Change
if ( userPassword == formPassword ){
to
if ( userPassword.equals(formPassword)){
In Java, String comparison is done with .equals(). Using == compares the object reference and not their values.
Java String comparison
In JAVA you should use equals to judge equal of value of different String.
== is used to judge object pointer for string, so it would only return false in your case.

i want to compare names in two different tables and then equal get details

i have two different tables.I want to compare the names in two tables,if both the names are equal then i want to get the details.how can i do that? please help me.
Here is the code:
Cursor cr=mDbManager.fetchnewcustomerdata();
Cursor cr1 = mDbManager.fetchinvoicecustomerdata();
cr1.moveToFirst();
//String address;
while (!(cr1.isAfterLast())) {
String name = cr1.getString(cr1.getColumnIndex("icstname"));
if(name.equals(cr.getString(cr.getColumnIndex("cname"))))
{
Toast.makeText(this,"equal",1000).show();
}
String address = cr1.getString(cr1.getColumnIndex("caddress"));
map.put(name, address);
nameAList.add(cr1.getString(cr1.getColumnIndex("icstname")));
cr1.moveToNext();
}
mDbManager.close();
}
I think two ways it is possible
1) Either you need to establish relationship between those tables using the name column you are thinking of same between two tables.
2) Two separate queries to database, where condition for both queries will be the name
You can use CursorJoiner
http://developer.android.com/reference/android/database/CursorJoiner.html
Firstly, the names are ordered .
Then, sort it as the following sample:
while( !eofInTable1 && !eofInTable2) {
String name1 = getFieldFromTable1();
String name2 = getFiledFromTable2();
int tmpResult = name1.compareTo(name2);
if(tmpResult == 0) {
doYourAction();
table1.moveToNext();
table2.moveToNext();
}
else if( tmpResult == -1) {
table1.moveToNext();
}
else {
table2.moveToNext();
}
}

Categories

Resources