0
public class dailyMenu          
{               

    private string day="";
    private int date = 0; 
    private static int nextDate=1;
    private string entree ="";
    private double price;
    private double calories;

    private static string [] daysOfWeek= {"Monday","Tuesday","Wednesday",
        "Thursday", "Friday", "Saturday", "Sunday"};

    private static string[] entrees = {"Beef Tenderloin Fresco", 
        "Madagascar Filet Mignon", "Filet Mignon", " Lobster Ravioli",
        "Asian Infused Braised Beef", "New Age Chicken Cordon Bleu", 
        "Short Ribs", " Beef Wellington","Fajitas", "Bacon Cheeseburger", 
        "Beef Burgandy", "Spagehetti"};

    private static double [] entreePrices= { 5.99,7.99,6.99,4.50,9.99,10.29,
        5.67,8.99, 3.99,4.78,10,79,6.98};

    private static int[] entreeMealCaloricVal= { 999,1288,770,699,450,999,1500,873, 911,
        1011, 777,500}; 

    public dailyMenu()
    {
        assignDate();
        GetDay();
        RandPopulate();
    }

    void assignDate()
    {
        date = nextDate;
        nextDate++;

        if (GetDay()== "Friday") 
        {
            nextDate += 2;
        }
    }

    void RandPopulate()
    {
        Random random = new Random();
        int randomNumber = random.Next(0,13);

        entree = entrees [randomNumber];
        price = entreePrices [randomNumber];
        calories = entreeMealCaloricVal [randomNumber];
    }
}

The IDE is telling me that line 56, 41, and 14 could be the problem so I'm guessing it has something to do with my random number generator.

Can someone give me a hand?

Matt C
  • 4,470
  • 5
  • 26
  • 44

1 Answers1

2

looks like entrees and entreeMealCaloricVal contain 12 items. That means they'll be indexed from 0 to 11.

Using random.Next(0,13) will generate a number from 0 to 12. When you try to access those arrays at index 12, your exception gets thrown.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • He has more than just this. He was also trying to call daysOfWeek[-1] in the GetDay function when the date was a multiple of 7. – ohiodoug Apr 21 '16 at 01:49
  • Good call, that method wasn't in the original code he posted though. – Jonesopolis Apr 21 '16 at 01:49
  • I asked to see it in the comments before counting items in the arrays. You had already answered the main culprit before I had a chance. =) – ohiodoug Apr 21 '16 at 01:51
  • Thank you guys so just change the 13 to a 12? – Nnacheta Nnyagu Apr 21 '16 at 01:51
  • And fix your getDay function – ohiodoug Apr 21 '16 at 01:52
  • And no, changing 13 to 12 is not the best solution. You want to change it to the length of your array. Something like `entrees.Length`. You should also look at creating an object for the data and drop it into one array rather than creating separate arrays. – ohiodoug Apr 21 '16 at 01:54
  • Alright I've done that but when i run my code to print it out the day of the week does not show up, but my entree price and calories do. – Nnacheta Nnyagu Apr 21 '16 at 01:58