-5

I have this error in my code:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 70
at Main.main(Main.java:72)

I don't know why it is doing this? please help. I thank you in advance. I have gone through several threads on several websites and it doesn't have anything that worked. I have been working on this for a while. I know what the ArrayIndexOutOfBoundsException is i am trying to figure it out though and i have been unsuccessful

This is the code:

import java.util.Scanner;

import java.util.Random;
public class Main
{
    public static void main (String[] args)
    {
        Scanner myScanner = new Scanner (System.in);
        Random myRandom = new Random();
        int answer;
        int definitionSelected = 0;
        String[] code = new String[68];
        String[] definition = new String[68];
        int[] listedDefinition = new int[4];
        int[] definitionUsed = new int[68];
        int i;
        int Size = 68;
        int Other = 4;
        double[] alpha = new double[68];
        int count = 0;
        code[0] = "10-0" ; definition[0] = "Disappeared";
        code[1] = "10-1" ; definition[1] = "Frequency Change";
        code[2] = "10-2" ; definition[2] = "Negative";
        code[3] = "10-3" ; definition[3] = "Stop Transmitting";
        code[4] = "10-4" ; definition[4] = "Affirmative";
        code[5] = "10-5" ; definition[5] = "Relay Message";
        code[6] = "10-6" ; definition[6] = "Busy";
        code[7] = "10-7" ; definition[7] = "Out Of Service";
        code[8] = "10-8" ; definition[8] = "In Service";
        code[9] = "10-9" ; definition[9] = "Repeat";
        code[10] = "10-10" ; definition[10] = "Fight In Progress";
        code[11] = "10-11" ; definition[11] = "Traffic Stop";
        code[12] = "10-12" ; definition[12] = "Standby";
        code[13] = "10-13" ; definition[13] = "Gun Shots";
        code[15] = "10-15" ; definition[15] = "Subject en Route to station";
        code[16] = "10-16" ; definition[16] = "Stolen Vehicle";
        code[17] = "10-17" ; definition[17] = "Suspicious Person";
        code[19] = "10-19" ; definition[19] = "Active Ride Along";
        code[20] = "10-20" ; definition[20] = "location";
        code[22] = "10-22" ; definition[22] = "Disregard";
        code[23] = "10-23" ; definition[23] = "Arrived on scene";
        code[25] = "10-25" ; definition[25] = "Do you have contact with ?";
        code[26] = "10-26" ; definition[26] = "ETA";
        code[27] = "10-27" ; definition[27] = "Drivers license check for valid";
        code[28] = "10-28" ; definition[28] = "license Plate check";
        code[29] = "10-29" ; definition[29] = "NCIC Warrant Check";
        code[30] = "10-30" ; definition[30] = "Wanted Person";
        code[31] = "10-31" ; definition[31] = "Armed Suspect";
        code[32] = "10-32" ; definition[32] = "Request Backup, Code 1,2,3";
        code[34] = "10-34" ; definition[34] = "Wanted Person";
        code[35] = "10-35" ; definition[35] = "Not Wanted No Warrants";
        code[38] = "10-38" ; definition[38] = "Suspicious Vehicle";
        code[41] = "10-41" ; definition[41] = "Beginning Tour Of Duty";
        code[42] = "10-42" ; definition[42] = "Ending Tour Of Duty";
        code[43] = "10-43" ; definition[43] = "Information About ?";
        code[46] = "10-46" ; definition[46] = "Attempt Of Suicide";
        code[49] = "10-49" ; definition[49] = "Drag Racing";
        code[50] = "10-50" ; definition[50] = "vehicle Accident";
        code[51] = "10-51" ; definition[51] = "Request Towing Service";
        code[52] = "10-52" ; definition[52] = "Request EMS";
        code[53] = "10-53" ; definition[53] = "Request Fire Department";
        code[54] = "10-54" ; definition[54] = "Hit And Run";
        code[55] = "10-55" ; definition[55] = "Intoxicated Driver";
        code[56] = "10-56" ; definition[56] = "Intoxicated Pedestrian";
        code[60] = "10-60" ; definition[60] = "Assault By An Animal";
        code[61] = "10-61" ; definition[61] = "Bicycle Theft";
        code[62] = "10-62" ; definition[62] = "Kidnapping";
        code[63] = "10-63" ; definition[63] = "Radio Check";
        code[64] = "10-64" ; definition[64] = "Sexual Assualt";
        code[65] = "10-65" ; definition[65] = "Armed Robbery";
        code[66] = "10-66" ; definition[66] = "Reckless Driver";
        code[67] = "10-67" ; definition[67] = "Fire";
        code[70] = "10-70" ; definition[70] = "Foot Pursuit";
        code[71] = "10-71" ; definition[71] = "Request Supervisor On Scene";
        code[72] = "10-72" ; definition[72] = "Administrative Assistance";
        code[73] = "10-73" ; definition[73] = "Advise Status";
        code[74] = "10-74" ; definition[74] = "Negative";
        code[76] = "10-76" ; definition[76] = "Prowler";
        code[80] = "10-80" ; definition[80] = "Vehicle Pursuit";
        code[85] = "10-85" ; definition[85] = "Delay Due To";
        code[86] = "10-86" ; definition[86] = "Any Traffic For me?";
        code[88] = "10-88" ; definition[88] = " Requesting Deputy Chief (Department)";
        code[89] = "10-89" ; definition[89] = "Requesting Chief (Department)";
        code[90] = "10-90" ; definition[90] = "Warning";
        code[91] = "10-91" ; definition[91] = "Unnecessary use of Radio";
        code[93] = "10-93" ; definition[93] = "Bank Alarm";
        code[97] = "10-97" ; definition[97] = "In route";
        code[99] = "10-99" ; definition[99] = "Officer In Distress EXTREME EMERGENCY ONLY";
        do
        {
            answer = myRandom.nextInt(68);
            System.out.println("\nCode Is :  " + code [answer] + "\n");
            for (i = 0;i<Size;i++) {
                int j;
                do 
                {
                    j = myRandom.nextInt(68);
                }
                while (definitionUsed[j] != 0 || j == answer);
                definitionUsed[j] = 1;
                listedDefinition[i] = j;

            }
            listedDefinition[myRandom.nextInt(4)] = answer;
            for (i = 0;i<Other;i++)
            {
                System.out.println((i + 1) + " - " +
                definition[listedDefinition[i]]);
            }
            System.out.print("\nWhat is The definition? (Enter 0 to Stop) ");
            definitionSelected = myScanner.nextInt();
            if (definitionSelected != 0)
            {
                if (listedDefinition[definitionSelected - 1] == answer)
                System.out.println("That's It Good Job");

            }
            else
            {
                System.out.println("Sorry, The answer is " + definition +".");
            }
        }
        while (definitionSelected != 0);
        }
}
npkllr
  • 556
  • 1
  • 5
  • 20
Quinn
  • 9
  • 2
  • Do you understand what the exception means? – Thorbjørn Ravn Andersen Mar 08 '19 at 15:06
  • Perhaps you might be able to reduce this to a [mcve]. – khelwood Mar 08 '19 at 15:06
  • 2
    Welcome Quinn to StackOverflow! It seems like you have initialized your arrays with a smaller size than you are using them. definition = new String[68] e.g. would only allow you to use definition till index [67] – TheRealVira Mar 08 '19 at 15:06
  • You have defined `code` and `definition` arrays with size 68, but you are trying `code[70] = "10-70"` and `definition[70] = "Foot Pursuit";`. Thats why you get this exception – Prasann Mar 08 '19 at 15:10
  • A dictionary might be a better fit here: [How do you create a dictionary in Java?](//stackoverflow.com/q/13543457). The "code" would be the key, and the "definition" would be the value. – 001 Mar 08 '19 at 15:10
  • `ArrayIndexOutOfBoundsException: 70 at Main.main(Main.java:72` <=> accessing **invalid** index `70` (first number of message) at line `72` (second number of message) in file `Main.java`, method `Main.main` – user85421 Mar 08 '19 at 15:20

2 Answers2

0

You have to fix initial array size.

In your code you have:

code[99] = "10-99"

and

definition[99] = "Officer In Distress EXTREME EMERGENCY ONLY";

In both cases you are trying to assing to position 99 so it means that you have at least 100 element (because for 100 elements positions are from 0 to 99).

So you have to change your initial size from 68 to 100 in those lines:

String[] code = new String[68];
String[] definition = new String[68];
int[] listedDefinition = new int[4];
int[] definitionUsed = new int[68];
double[] alpha = new double[68];

to:

String[] code = new String[100];
String[] definition = new String[100];
int[] listedDefinition = new int[100];
int[] definitionUsed = new int[100];
double[] alpha = new double[100];
Boken
  • 4,825
  • 10
  • 32
  • 42
  • Hey Now it gives me this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Main.main(Main.java:100) – Quinn Mar 08 '19 at 16:27
  • Did you changed also size of `listedDefinition` array? – Boken Mar 08 '19 at 22:09
0

You have defined an array of size 68 and you are trying to use it beyond this size. Since size of array cannot be dynamically increased. So we get the exception, Array index out of bounds. If you want the size to change dynamically use ArrayList.

  • How would i use array list i just imported it – Quinn Mar 08 '19 at 16:53
  • Couldn't get you. I just explained the reason for which it is throwing the error and just suggested a better way for you to approach. Is there any issue with that @Quinn ? – Prerna Gandhi Mar 11 '19 at 06:29