Ian
Ian

Reputation: 1490

for loop doing nothing on first run? possible programming logic error?

Basically, in the method below, for reasons unknown to myself or my team, the outermost for loop will does not execute any code on the first itteration. What I mean by that is, on the first round of execution it goes to the end of the loop and back to the beginning without executing any code. I have confirmed this by debugging through eclipse. However, the loop executes as normal after this first cycle.

What this method is doing is pulling days from a database, putting them into weeks, and putting those weeks into a month. but, because of this weird "glitch" the first cycle does not pull any days and subsequently does not create a week to be put in the month BUT it does execute the counter of the loop which basically leaves the data out off sync with the loop:

1st run: nothing
2nd run: 1st week
3rd run: 2nd week
...
last run: SECOND LAST WEEK

so, everythime the method returns the month, the last week is always missing.

BUT what is really WEIRD is that this loop used to work perfectly. In fact, I am 99.9999% positive that it was functioning before I went home last night and then when I came in today it suddenly developed this error. The only reason that I have that tiny amount of doubt is that IT MAKES ABSOLUTELY NO SENSE FOR THAT TO BE THE CASE! So realistically I must have accidently changed something and not realised it. If anyone could thake a look at this and suggest a possible cause that would be very much appreciated.

Thanks

private Month translateToFormData(List<WeekEntry> weekList, TimeSheetForm timeSheetForm) {
    int monthOfInterest = timeSheetForm.getMonth().getMonthOfYear();
    // month to be returned
    Month month = new Month();
    // Calendars to translate from Date to Calendar
    // loop through weekList
    for (WeekEntry weekEntry : weekList) {
        // week value to be filled
        Week week = new Week();
        Calendar calWeekBeginning = new GregorianCalendar();
        // set the date of the first day in "week" (Sunday)
        calWeekBeginning.setTime(weekEntry.getWeekBeginning());
        week.setWeekBeginning(calWeekBeginning);
        Set<DayEntry> daySet = weekEntry.getDays();
        // Loop through days of the weekList items
        for (DayEntry dayEntry : daySet) {
            // day value to be filled
            Day day = new Day();
            Calendar calDateOfDay = new GregorianCalendar();
            // set the date of the day
            calDateOfDay.setTime(dayEntry.getDateOfDay());
            day.setDate(calDateOfDay);
            // set the hours of the day
            day.setWorkHours(dayEntry.getHours());
            // define the day type
            if (calDateOfDay.get(Calendar.MONTH) != monthOfInterest) {
                day.setType(DayType.BLANK_DAY);
            } else if (calDateOfDay.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calDateOfDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                day.setType(DayType.WEEK_END);
            } else {
                day.setType(DayType.WEEK_DAY);
            }
            // add the day to the week
            week.addDay(day);
        }
        // add the week to the month
        month.addWeek(week);
    }
    return month;
}

Upvotes: 0

Views: 368

Answers (2)

Ian
Ian

Reputation: 1490

Found the problem, it was me being a herpaderp! thanks for your suggestions

Edit at the request of chris...

As it turned out, it was to do with how I was saving the objects to the DB. It was putting a timestamp on each row as well at the date and this was causing it to not retrieve the first element if the code ran at an earlier time of day then when the entries were initially saved. I don't know why it was only the first cycle of the loop that was affected but once I set it to save items without recording the time of day, it runs without any problems.

Upvotes: 0

jasonk
jasonk

Reputation: 969

I'd write a comment but I don't have points. You'd need to provide more code I think. You should send the contents of weekList and TimeSheetForm. Any changes to those classes?

(To think about - this sort of issue is a great example of why you need automated test cases; you'll be able to see as soon as it fails.)

Upvotes: 1

Related Questions