如果在执行过程中语句被跳过 [英] If statement being skipped during execution

查看:50
本文介绍了如果在执行过程中语句被跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这段代码有疑问.在执行过程中,第6行的if语句将被忽略.至此,我已经逐步完成了代码,变量file [position]的值是"subjects.dat".但是,它跳过了其中的步骤,而转到相关的else语句.有什么想法吗?

I am having a problem with this piece of code. The if statement on line 6 is being ignored during execution. I have stepped through the code at this point and the value of the variable file[position] is "subjects.dat". However, it is skipping over the steps in this and going to the related else statement. Any ideas why??

dialogButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (markComplete.isChecked()) {
                    String toDelete;
                    String[] files = fileList();
                    if (files[position] == "subjects.dat") { //the error occurs at this line
                        toDelete = files[position + 1];
                        boolean deleted = deleteFile(toDelete);
                        if (deleted) {
                            dialog.dismiss();
                        } else {
                            // Do nothing
                        }
                    } else {                            
                        toDelete = files[position];                     
                        boolean deleted = deleteFile(toDelete);
                        if (deleted) {
                            dialog.dismiss();
                        } else {
                            //Do nothing
                        }   
                    }

                }                   
            }

谢谢!

推荐答案

您需要使用 .equals()比较两个字符串的实际值-否则,您正在检查它们是否为相同的对象.

You need to use .equals() to compare the actual value of two strings - otherwise you're checking if they're the same object.

if (files[position].equals("subjects.dat")) { 
    // do stuff
}

这篇关于如果在执行过程中语句被跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆