Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two arrays arr for the 6 values str,dex,con,int,wis,cha and a string array 'attr' for these 6 attributes.for each 6 values I'm trying to get the total of four numbers which user inputs and drop minimum from the total.but when I run the program I get an error 'ArrayIndexOutOfBoundsException'when I try to enter 4 values for the wis integer which is last of arr array.

What I have tried:

<pre>public static int[] diceTotal(){
        for(int i=0;i<arr.length;i++){
            int sum=0;
            int[] value = {0,0,0,0};
            for (int j=0;j<value.length;j++){
                out.println("Enter " + attr[i] + " value:");
                value[i] = GetAnIntegera();
                while ((value[i] < 1) || (value[i] > 6)) {
                    out.println("Invalid Attribute value. Enter again.:");
                    value[i] = GetAnIntegera();
                }
            }
            int min=value[0];
            for(int k=0;k<value.length-1;k++){
                if(value[i]<min){
                    min=value[i];
                }
            }for (int l=0;l<value.length;l++){
                sum+=value[i];
            }arr[i]=sum-min;
        }
        return arr;}
Posted
Updated 10-Apr-18 23:50pm

Because you are using i not j to access the array value - and since i varies from 0 to the number of elements in arr the chances are that arr is bigger than value

In cases like this, use the debugger - it can show you exactly what is going on and help you find out why a lot quicker and easier than asking here...
 
Share this answer
 
v2
Hi,

First:
Please keep in your mind that arrays in C and C based languages like C# or Java starts from 0 so the actual length of the array is always length -1, thus you need to recode your loops based on this assumption.

Second:
Try to avoid accessing different arrays from irrelevant indexer like using j to access i or vice versa.

Third:
You are assuming that the length of 'arr' would always equal to the length of 'value' this is not a clean code.

BR,
A.H.
 
Share this answer
 
v2
The 'ArrayIndexOutOfBoundsException' you are recieving should state the line in which the exception occurs (see StackTrace). You should also setup the Exception Debug settings in VS
(see The New Exception Settings Window in Visual Studio 2015 – Microsoft DevOps Blog[^] ) to stop when the exception is being thrown.

This should enable you to see in the debug mode where and why you are receiving this exception.
 
Share this answer
 
Quote:
for(int i=0;i<arr.length;i++){
int sum=0;
int[] value = {0,0,0,0};
for (int j=0;j<value.length;j++){
out.println("Enter " + attr[i] + " value:");
value[i] = GetAnIntegera();


Since i=0..5, you cannot index value with it.
 
Share this answer
 
Your loops are using i, j, k, l as counters, but your code is only using i.
And since i depend on array arr of size 6 and you use it to access array value of size 4, you end up trying to access the fifth element of value, which doesn't exist.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900