简体   繁体   中英

Android issues with string comparison

I think I'm working too late and losing it but the string comparison in Android doesn't seem to work, probably it is just late.

I have a simple array of JSON objects and when my auto value is set to "0", I can compare that value to a String , when the auto is set to "1" the comparison goes straight into Exception ?

I tried equals and compareTo with the same result. Are there other ways of comparing string on android that is more appropriate?

Thank you

private int findAutoPlayIndex(JSONArray items){
    try{
        int returnValue = -1;
        final int size = items.length();
        for (int i = 0; i < size; i++) {
            JSONObject jObjectResult = items.getJSONObject(i);

            String auto = items.getJSONObject(i).getString("auto").trim();

            //if ( auto.equals("1") ){
            //    return i;
            //}

            if ( auto.compareTo("1") == 0){
                return i;
            }
        }

        return returnValue;
    } catch (Exception e) {
        e.printStackTrace();
        return  -1;
    }
}

在此处输入图片说明

Strange no stack ... nothing, IDE steps into that code but I can't see an exception variable or a stack

在此处输入图片说明

试试这个

if(Integer.parseInt(auto) == 1)

Try this:

if ("1".equals(auto)) {
    return i;
}

This will also handle null pointer exception in case auto has no value or if its null.

compareTo method is described as follows:

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Note that null is not an instance of any class, and null.compareTo("1") should throw a Null Object Refference even though null.equals("1") [if you get a null for auto it will crash]

int java.lang.String.compareTo(java.lang.String)' on a null object reference

so 0 compareTo 1 will return you -1 meaning obj is less than to 1

But make sure you don't get a null value to auto

To make sure what you get check inside your conditions like the example if its android use Logs

String auto = "1";
if(auto == null){
        System.out.println("MyOutPut-> is null");
    }
    else if ( auto.compareTo("1") == 0){
        System.out.println("MyOutPut->"+auto.compareTo("1"));
    }
    else {
        System.out.println("MyOutPut->" + auto.compareTo("1"));
    }

warning : Also for the Exception you are returning (-1 ).

But remember as i said compare to can rerun negative int. So -1 is possible. You might think it as an error because you return -1 when there's an exception but it might be the real value that you get from compareTo funtion.

try this :

if (auto.toString().equals("1")){
    return i;
}

hope it helps

Ok, that was totally silly. When stepping in debug mode in IDE, in my if equals logic I just had a one statement that is return i.

The comparison was working OK the IDE automatically skips next step to the last return statement in the function which in my case was in the exception handler.

That was why I didn't see an actual exception variable. You learn something new every day. Thank you for all your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM