0

I am getting the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
        at score.checkWinnings(score.java:29)
        at yahtzee.main(yahtzee.java:52)

Line 52 in main is:

points.checkWinnings(Dice, wins);

Line 29 in score is:

if (Dice[y] == 1) 

I am trying to get the score of dice rolled but am not sure why lines 29 and 52 are messing it up. I have tried:

for (y = 0; y < Dice[y]; y++);
//this one still gives me an error
for (y = 0; y < 5; y++);
//this one only works when I input that I want to use 5 dice

Would anyone know why what I have/ what i've tried isn't working? Any help would be greatly appreciated!

I have included the relevant sections of my code below:

Main:

import java.util.ArrayList;

public class yahtzee
{
    public static void main(String[] args) 
  {
    int play = 1, scorea = 0, sum = 0;
    int[] wins = new int[15];
        
    while ((play == 1) && (sum < 15)) 
    {
      yahtzeeConfig config= new yahtzeeConfig();
      config.start();
      int[] Dice = new int[config.hand];
      //hand in score 
      sum = 0;;
      int roll = 0;
      int x, z;
      int rerolla = 0;
      dice die = new dice();
      
      System.out.println("\nHere is your roll:\n");
      //sets the dice values
      ArrayList<Integer> arList= new ArrayList<Integer>(config.hand); 
      for (x = 0; x < config.hand; x++) 
      {
        die.roll();
        Dice[x] = die.get();
        arList.add(Dice[x]);
      }
      //prints out dice values
      for (int i =0; i< config.hand; i++)
      {
        System.out.println("Die " + (i+1) + ": " + Dice[i]);
      }
      //re-rolls dice 
      do {
        for (int i =0; i< config.hand; i++)
      {
        play = inputInt("\nWould you like to reroll " + Dice[i] + " ? (1=yes, 2=no)");
        if (play == 1)
        {
          Dice[i]= die.roll();
          //roll++;
        }
        System.out.println("Die " + (i+1) + ": " + Dice[i]);
      }
    }
    //checks score
    while ((roll < 2) && (rerolla > 0));
      score points = new score();
      points.checkWinnings(Dice, wins);
      for (z = 0; z < 15; z++) {
        sum += wins[z];
      }
      scorea += points.return_score();
      System.out.println("Your total score is: " + scorea);

Score Class:

public class score
{
    private int score;
 
  public score() 
  {
    score = 0;
  }
 
  public void checkWinnings(int[] Dice, int[] wins) 
  {
    
    System.out.println("\nLet's Check Score: \n ");
 
    int x = 0, y = 0, winings = 0, winingsa = 0;
    int ones = 0, twos = 0, threes = 0, fours = 0, fives = 0, sixes = 0;
 
    //counts numbers
    for (y = 0; y < Dice.length; y++); 
    //for (y = 0; y < Dice[y]; y++);
    //for (y = 0; y < 5; y++); 
    {
      if (Dice[y] == 1) 
      {
        ones++;
      }
      if (Dice[y] == 2) 
      {
        twos++;
      }
      if (Dice[y] == 3) 
      {
        threes++;
      }
      if (Dice[y] == 4) 
      {
        fours++;
      }
      if (Dice[y] == 5) 
      {
        fives++;
      }
      if (Dice[y] == 6) 
      {
        sixes++;
      }
    }
    //upper total
    System.out.println ("Ones: " + ones);
    System.out.println("Twos: " + twos);
    System.out.println("Threes: " + threes);
    System.out.println("Fours: " + fours);
    System.out.println("Fives: " + fives);
    System.out.println("Sixes: " + sixes + "\n");
}
Y Hashida
  • 1
  • 3
  • What do you expect `y < Dice[y]` to actually do? What if you happen upon the dice sequence of `{6, 5, 4, 3, 2}`, for example, where `y < Dice[y]` evaluates to `0 < Dice[6]` when `Dice`'s highest index is 4? – Green Cloak Guy Mar 06 '21 at 22:49
  • Did you step through the code in a debugger? That is the 1st thing to do. – OldProgrammer Mar 06 '21 at 22:50
  • I made my dice variables into an ArrayList and thought that '.length' would be able to get the right number of dice based on the user input. I am confused because I am not sure how lines 52 and 29 have anything to do with the part I am trying to fix. – Y Hashida Mar 06 '21 at 23:03
  • 3
    remove the semi-colon after the `for` statement (`for (y = 0; y < Dice.length; y++); ` - it causes the loop to only iterate over the *empty statement*) –  Mar 06 '21 at 23:09
  • @user15244370 thank you! it fixed my error.. but why does the semi-colon affect this? ( I am hoping to understand) – Y Hashida Mar 06 '21 at 23:11
  • 1
    @YHashida When the loop ends, since `y` wasn't declared as part of the loop, it still exists after the loop, and has the value that caused the loop end, i.e. `y = Dice.length`. The following code then does `Dice[y]`, which of course fails immediately. --- Unless there are special circumstances, you should always declare the loop variable in the loop. It helps prevent errors like this. – Andreas Mar 06 '21 at 23:26
  • A for-loop executes the body of the loop. By putting a semicolon right after the loop condition, you are using an empty body. Normally the body is inclosed in braces. – NomadMaker Mar 06 '21 at 23:37

1 Answers1

0

Lots of good debugging in comments - your question is why are you getting ArrayIndexOutOfBounds - because arrays are 0'indexed in java...

i.e. an array of size 5 is able to be accessed via [0], [1], [2], [3], [4] only.

The specific reason you have the wrong index - I think a few people found for you already.

Generally an unexpected IndexOutOfBoundsException is quite likely to be an indicator of a bug (either values relied on that aren’t actually matching expectations OR an algorithmic problem).

Mr R
  • 754
  • 7
  • 19