简体   繁体   English

字符串比较的Android问题

[英]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. 我认为我工作太晚了并且丢失了它,但是Android中的字符串比较似乎不起作用,可能只是来晚了。

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 ? 我有一个简单的JSON对象数组,当我的auto值设置为“ 0”时,我可以将该值与String进行比较,当auto设置为“ 1”时,比较直接进入Exception吗?

I tried equals and compareTo with the same result. 我尝试了equalscompareTo具有相同的结果。 Are there other ways of comparing string on android that is more appropriate? 还有其他比较android上的字符串更合适的方法吗?

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 奇怪的是没有堆栈...什么都没有,IDE进入了该代码,但是我看不到异常变量或堆栈

在此处输入图片说明

试试这个

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. 如果auto没有值或它的值为null,这还将处理null指针异常。

compareTo method is described as follows: compareTo方法描述如下:

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] 请注意 ,null不是任何类的实例,并null.compareTo("1")应该抛出一个Null Object Refference即使null.equals("1") [如果你得到一个空自动就会死机]

int java.lang.String.compareTo(java.lang.String)' on a null object reference int java.lang.String.compareTo(java.lang.String)'对一个空对象的引用

so 0 compareTo 1 will return you -1 meaning obj is less than to 1 所以0 compareTo 1将返回-1,表示obj小于1

But make sure you don't get a null value to auto 但是请确保您没有将auto

To make sure what you get check inside your conditions like the example if its android use Logs 为了确保您在条件之内检查了什么,例如示例,如果其android使用日志

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 ). 警告 :同样对于异常,您将返回(-1)。

But remember as i said compare to can rerun negative int. 但是请记住,正如我所说的,比较可以重新运行负数int。 So -1 is possible. 因此-1是可能的。 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. 您可能会认为这是一个错误,因为在发生异常时返回-1,但这可能是您从compareTo函数获得的实​​际值。

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. 在IDE中进入调试模式时,在我的if等于逻辑中,我只有一条语句返回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. 比较工作正常,IDE会自动将下一步跳至该函数的最后一个return语句,在我的情况下,该语句位于异常处理程序中。

That was why I didn't see an actual exception variable. 这就是为什么我没有看到实际的异常变量的原因。 You learn something new every day. 你每天学习新的东西。 Thank you for all your help. 谢谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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