-4

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.

the_redcar
  • 129
  • 1
  • 1
  • 10
  • This is not a duplicate, using .equals() did not solve the problem. – the_redcar Oct 24 '19 at 05:10
  • 4
    Use breakpoints to check what's wrong. It's simply not possible that `if` condition will be called even if it's false. – Ranjan Oct 24 '19 at 05:32
  • 1
    Why do you believe the *"IF statement is being called"*? I see no log output or any description of why you concluded that. – Andreas Oct 24 '19 at 05:38
  • 1
    "is a String that has the specific value "Goal Time Not Specified."" this means that it is equals, thus the if must be called – barotia Oct 24 '19 at 05:44
  • Sorry for the very late response, and sorry for the mistakes in my question. Using breakpoints helped me find an obvious logical error that I should have caught before posting this question. – the_redcar Oct 25 '19 at 01:44

1 Answers1

2

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?

barotia
  • 428
  • 4
  • 12
  • Sorry for the very late response, and sorry for the typos in my question. Using breakpoints helped me find an obvious logical error that I should have caught before posting this question. Thank you so much for helping me solve my problem. – the_redcar Oct 25 '19 at 01:44