Well, I have finished one of my programs and have been tracing it over and over. Trying to figure out my problem and I just can't seem to figure it out so I have decided to try and let another pair of eyes look at it. A very simple program.

The idea of it is to let the user enter the sides of a dice, the number of dice to roll, and the number of rolls.

/*Kevin, september 14
 * DiceRolls.java from Chapter 10
 * Generate counts of dice roll outcomes.
*/

 /**
 * Dice are rolled and the outcome of each roll is counted.
 */

 import java.util.Scanner;
 import java.util.Random;

 public class DiceRolls1 {

       public static void main(String[] args) {
               int[] outcomes;       
               
               Scanner input = new Scanner(System.in);
               
               int numRolls;
               int sidedDice;
               int outcome;
               int max;
               
               int numDiceRolled;
               
               Random rand = new Random(); 
               
               outcome = 0;
               
               System.out.println("Enter the number of sides on the dice ");
               sidedDice = input.nextInt();
                
               System.out.println("Enter the number of dice to roll"); 
               numDiceRolled = input.nextInt();
                  
               /* prompt user for number of rolls */
               System.out.println("How many rolls? ");
               numRolls = input.nextInt();
               
               max = (sidedDice * numDiceRolled + 1);
               
               outcomes = new int[max];  //array with indices whatever the possibilities are 
    
           for (int roll = 0; roll < numRolls; roll++) { 
      
                 /* roll dice and add up outcomes */
                 for (int die = 0; die < numDiceRolled; die++) {
                         outcome += ( rand.nextInt(sidedDice) + 1);
                         outcomes[outcome] += 1; 
                 }
                }
  
               /* show counts of outcomes */
               
               for (int i = numDiceRolled; i <= max; i++) { 
                       System.out.println(i + ": " + outcomes[i]); 
               }
       }
}

It would be nice if we knew what the problem was (saves us time figuring out where to start).

True, sorry....Well, it compiles just fine and I am able to run it. But whenever I run it and enter in values it says "Exception in thread "main" java.lang.ArrayIndexOutofBoundsException: 7 at DiceRolls1.main<DiceRolls1.java:57"

Anyway, not sure if thats helpful or not..

I think the problem is in this code

for (int die = 0; die < numDiceRolled; die++) {
                         outcome += ( rand.nextInt(sidedDice) + 1);
                         outcomes[outcome] += 1; 
                 }

You use outcome += (rand.nextInt(sidedDice) + 1); . I think it's the += thats killing you. Once you have more than a single roll, there's the possibility that outcome will be larger than the valid index range of the outcomes array. Then you get your exception. Try changing the += to just = and see how it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.