0

I'm writing Java code for an old style phone plan, so I have:

  • a Band class: public Band(LocalTime startTime, LocalTime endTime, DayOfWeek[] combinedDays, double intervalCost)
  • a Rate class: public Rate(String name, Band[] bands, int intervalMs, double startCost, String numberRoot)

I want to write a private Band[] selectBandsInDay(DayOfWeek day) method inside the Rate class that, given a day of week, returns an array of Band composed of the bands of that day of week.

What I wrote was:

private Band[] selectBandsInDay(DayOfWeek day) {
        
        Band[] bandsInDay = new Band[bands.length];
        int size = 0;
        
        for (int i=0; i<bands.length; i++) {
            for (int j=0; j<bands.length; j++) {
                if (bands[j].getCombinedDays()[i] == day) {
                bandsInDay[size] = bands[i];
                size++;
                }
            }
        }       
        return bandsInDay;
    }

But I keep getting an Index Out Of Bounds exception (index 2 out of bounds for length 2).

How could I fix this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
chiagger
  • 63
  • 1
  • 8
  • 1
    What does `getCombinedDays()` return? Which line is throwing the exception? Have you debugged through your code? – Jon Skeet Apr 16 '21 at 13:35
  • `size` could be a problem here. Should the `size` reset to 0 once `i` increments? – firsttry Apr 16 '21 at 13:39
  • getCombinedDays() returns combinedDays parameter of Band class, which is an array of DayOfWeek. Line `if (bands[j].getCombinedDays()[i] == day) { ` is throwing the exception. – chiagger Apr 16 '21 at 13:40
  • @firsttry it shouldn't, but how can I avoid it resetting to 0? – chiagger Apr 16 '21 at 13:43
  • Both your loops use the length of your outer array to determine when to stop. You probably need to change `j – OH GOD SPIDERS Apr 16 '21 at 13:45
  • A dry run should be good enough to find out the problem. – Mugdha Apr 16 '21 at 13:49
  • How do you know that the maximum number of `bandsInDay` should be `bands.length`? Either you do know this number in advance but your code is producing more results than you expected, or you don't know this in advance in which case `bandsInDay` should probably be an empty list which you simply append to. – jarmod Apr 16 '21 at 14:03

1 Answers1

0

I had to go with a different approach.

private Band[] selectBandsInDay(DayOfWeek day) {
        
        int i = 0;
        int length = 0;
        for (Band band : bands) {
            if(DayOfWeekHelper.isDayIn(day, band.getCombinedDays()))
                length++;
        }
        Band[] bands_1 = new Band[length];
        for (Band band : bands) {
            if(DayOfWeekHelper.isDayIn(day, band.getCombinedDays()))
                bands_1[i++] = band;
        }
        return bands_1;
    }

And I created that DayOfWeekHelper class with isDayIn() method:

public class DayOfWeekHelper {
    public static boolean isDayIn(DayOfWeek day, DayOfWeek[] combinedDays) {
        for (DayOfWeek d : combinedDays) {
            if (d == day)  {
                return true;
            }
        }
        return false;
    }
}
chiagger
  • 63
  • 1
  • 8