0

I have a method in Java class file "Day" called enoughTimeChecker. Here is the code for the class

package com.example.calendarviewexample;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.time.LocalDate;
import java.util.ArrayList;

public class Day extends AppCompatActivity
{
    private LocalDate date;
    private int totalTime;




    public Day(LocalDate date, int totalTime)
    {
        this.date = date;
        this.totalTime = totalTime;
    }

    public static ArrayList<Day> listOfDayObjects()
    {
        ArrayList<Day> listOfDayObjects = new ArrayList<>();
        return listOfDayObjects;
    }



    public static void enoughTimeChecker(LocalDate date, int addedTime)
    {
        for(int i =0; i<listOfDayObjects().size(); i++)
        {
            if (listOfDayObjects().get(i).getDate() == date)
            {
                int x = listOfDayObjects().get(i).getTotalTime();
                listOfDayObjects().get(i).setTotalTime(x + addedTime);
                Log.d("addedTime", "total time of " + String.valueOf(listOfDayObjects().get(i).getDate()) + "is" + String.valueOf(x + addedTime));
            }
        }

    }

    public static void setDayClass(LocalDate currentDate)
    {
        currentDate = LocalDate.now();
        for (int i =0;  i<366; i++)
        {
            Day newDay = new Day(currentDate, 0);
            listOfDayObjects().add(newDay);
            currentDate = currentDate.plusDays(1);
            Log.d("DayClazz", "Class created " + String.valueOf(currentDate) );
        }

    }

    public LocalDate getDate()
    {
        return date;
    }

    public void setDate(LocalDate date)
    {
        this.date = date;
    }

    public int getTotalTime()
    {
        return totalTime;
    }

    public void setTotalTime(int totalTime)
    {
        this.totalTime = totalTime;
    }
}

In the file "EventEditActivity", "enoughTimeChecker" is called from method "Save Action". Here is the Save Action function

public void SaveAction(View view)
    {
        String taskName = eventNameET.getText().toString();
        Task newTask = new Task(taskName, CalendarUtils.selectedDate, time, calculatedTime);
        /// this is where the attributes of the task are set
        Day.enoughTimeChecker(CalendarUtils.selectedDate, calculatedTime);
        Task.taskList.add(newTask);

        
        finish();


    }

It should output in the logcat when a task is added but it just doesn't - there's no error message when enoughTimeChecker is called.

I have no idea what the error is, I think there might be something wrong with the array perhaps. I made the array in the Day class file here

public static ArrayList<Day> listOfDayObjects()
    {
        ArrayList<Day> listOfDayObjects = new ArrayList<>();
        return listOfDayObjects;
    }

and I made the list of objects here

for (int i =0;  i<366; i++)
{
    Day newDay = new Day(currentDate, 0);
    listOfDayObjects().add(newDay);
    currentDate = currentDate.plusDays(1);
}

any help would be appreciated

Suf1an65
  • 1
  • 1
  • Can you please post the whole code for `Day` class? – m0skit0 Feb 15 '23 at 19:40
  • Log.d is inside of 2 conditionals (the for() loop and an if statement). Have you run it under the debugger? My guess is one of those two conditionals are not doing what you think they are or possibly enoughTimeChecker is not really getting called. Whatever, the answer is in the debugger. – JJF Feb 15 '23 at 19:43
  • Can you elaborate on how I would go about debugging it, there are no technical errors and I'm quite new to Android Studio – Suf1an65 Feb 15 '23 at 19:53
  • Is `newTask` supposed to execute `Day.enoughTimeChecker(...)`? If so, you haven't written your code so that happens. What is the package name for `Task`? – pfurbacher Feb 15 '23 at 19:55
  • new task isn't meant to execute Day.enoughTimeChecker(), it just creates an object from the Java Class "task", Day.enoughTimeChecker() is simply meant to be executed using some of the attributes of the newtask – Suf1an65 Feb 15 '23 at 19:59
  • Add a log statement to the start and/or end of `enoughTimeChecker` to narrow down and confirm the source of the problem. – Solomon Ucko Feb 15 '23 at 20:10
  • `listOfDayObjects()` is returning a new empty list on each call, `enoughTimeChecker()` will never do anything since iterating (or not iterating at all) over this **empty** list (`listOfDayObjects().size()` will always return `0`) – user16320675 Feb 15 '23 at 20:26
  • 1
    I did your suggestion with the log statements, it returns the log at the beginning when the function is called but won't after the for loop so the problem lies around there. – Suf1an65 Feb 15 '23 at 22:05
  • 1
    @user16320675, you were correct, it returns an empty list. Thank you very much – Suf1an65 Feb 15 '23 at 22:12

1 Answers1

0

Use Object#equals(Object) instead of == to compare objects; the problem is on this line:

listOfDayObjects().get(i).getDate() == date

See What is the difference between == and equals() in Java? for more information.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
  • 1
    thanks, the function still doesn't return the log but that should definitely get me a step closer – Suf1an65 Feb 15 '23 at 20:16