简体   繁体   中英

IF statement being run even though expression is false

I am creating an Android Studios tasklist app. For some reason, an IF statement is being called even though the expression is false, and thus producing errors at runtime. Code is provided below:

if (taskArray5.get(0).equals( "Goal Time Not Specified") ) {

    Log.d("hello", taskArray5.get(0).getClass().toString());
    Log.d("hello", taskArray5.get(0));
    String[] localTimeList = localTime.split(":");
    String previouslySetTime = taskArray5.get(0).substring(0, taskArray5.get(0).length() - 5);
    String[] previouslySetTimeList = previouslySetTime.split(":");
    Integer localTimeHours = Integer.parseInt(localTimeList[0]);
    Integer localTimeMinutes = Integer.parseInt(localTimeList[1]);
    Integer localTimeSeconds = Integer.parseInt(localTimeList[2]);
    char AMORPM = taskArray5.get(0).charAt(taskArray5.get(0).length() - 4);
    Integer previouslySetTimeHours;
    if (AMORPM == 'A') {
        previouslySetTimeHours = Integer.parseInt(previouslySetTimeList[0]);
    } else {
            previouslySetTimeHours = Integer.parseInt(previouslySetTimeList[0]) + 12;
    }

I used Log.d to confirm that taskArray5.get(0) is a String that has the specific value "Goal Time Not Specified." In addition, using the equals() function also did not solve the problem. What am I doing wrong? Any help is appreciated.

Things to do in this case:

Use breakpoints, simply logging sometimes is not enough, you can miss something, ie, the if is not called when you think it is called or it could be called multiple times

You wrote: taskArray.get(0) is a String that has the specific value "Goal Time Not Specified.", however you used taskArray5 in the code. Also if it has the specific value, which is required for the condition, is not normal that for that to enter?

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