I have a ArrayIndexOutOfBoundsException error [duplicate] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceWhat causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?Can an abstract class have a constructor?Why does Java have transient fields?Why do this() and super() have to be the first statement in a constructor?Error java.lang.OutOfMemoryError: GC overhead limit exceeded'Must Override a Superclass Method' Errors after importing a project into EclipseDoes JavaScript have a method like “range()” to generate a range within the supplied bounds?How can I “delimit” an integer from a given string?Can a scanner read input of different typesWhy doesn't RecyclerView have onItemClickListener()?Having a runtime error of ArrayIndexOutOfBoundsException: 97

Is it possible to ask for a hotel room without minibar/extra services?

When is phishing education going too far?

Passing functions in C++

What is the electric potential inside a point charge?

What loss function to use when labels are probabilities?

Stop battery usage [Ubuntu 18]

What was the last x86 CPU that did not have the x87 floating-point unit built in?

Need a suitable toxic chemical for a murder plot in my novel

When communicating altitude with a '9' in it, should it be pronounced "nine hundred" or "niner hundred"?

What do you call a plan that's an alternative plan in case your initial plan fails?

Direct Experience of Meditation

Blender game recording at the wrong time

How do you clear the ApexPages.getMessages() collection in a test?

What is the order of Mitzvot in Rambam's Sefer Hamitzvot?

Antler Helmet: Can it work?

Slither Like a Snake

Mortgage adviser recommends a longer term than necessary combined with overpayments

Determine whether f is a function, an injection, a surjection

Jazz greats knew nothing of modes. Why are they used to improvise on standards?

Can a monk deflect thrown melee weapons?

How to rotate it perfectly?

What do I do if technical issues prevent me from filing my return on time?

Autumning in love

How do I automatically answer y in bash script?



I have a ArrayIndexOutOfBoundsException error [duplicate]



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceWhat causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?Can an abstract class have a constructor?Why does Java have transient fields?Why do this() and super() have to be the first statement in a constructor?Error java.lang.OutOfMemoryError: GC overhead limit exceeded'Must Override a Superclass Method' Errors after importing a project into EclipseDoes JavaScript have a method like “range()” to generate a range within the supplied bounds?How can I “delimit” an integer from a given string?Can a scanner read input of different typesWhy doesn't RecyclerView have onItemClickListener()?Having a runtime error of ArrayIndexOutOfBoundsException: 97



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-5
















This question already has an answer here:



  • What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    24 answers



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++) 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);











share|improve this question















marked as duplicate by khelwood, MC Emperor, Arnaud, Paolo Forgia, Stephen C arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 15:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Do you understand what the exception means?

    – Thorbjørn Ravn Andersen
    Mar 8 at 15:06











  • Perhaps you might be able to reduce this to a Minimal, Complete, and Verifiable example.

    – khelwood
    Mar 8 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 8 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 8 at 15:10











  • A dictionary might be a better fit here: How do you create a dictionary in Java?. The "code" would be the key, and the "definition" would be the value.

    – Johnny Mopp
    Mar 8 at 15:10


















-5
















This question already has an answer here:



  • What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    24 answers



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++) 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);











share|improve this question















marked as duplicate by khelwood, MC Emperor, Arnaud, Paolo Forgia, Stephen C arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 15:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Do you understand what the exception means?

    – Thorbjørn Ravn Andersen
    Mar 8 at 15:06











  • Perhaps you might be able to reduce this to a Minimal, Complete, and Verifiable example.

    – khelwood
    Mar 8 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 8 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 8 at 15:10











  • A dictionary might be a better fit here: How do you create a dictionary in Java?. The "code" would be the key, and the "definition" would be the value.

    – Johnny Mopp
    Mar 8 at 15:10














-5












-5








-5









This question already has an answer here:



  • What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    24 answers



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++) 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);











share|improve this question

















This question already has an answer here:



  • What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    24 answers



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++) 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);






This question already has an answer here:



  • What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

    24 answers







java arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 16:40









npkllr

25214




25214










asked Mar 8 at 15:03









QuinnQuinn

21




21




marked as duplicate by khelwood, MC Emperor, Arnaud, Paolo Forgia, Stephen C arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 15:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by khelwood, MC Emperor, Arnaud, Paolo Forgia, Stephen C arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 15:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Do you understand what the exception means?

    – Thorbjørn Ravn Andersen
    Mar 8 at 15:06











  • Perhaps you might be able to reduce this to a Minimal, Complete, and Verifiable example.

    – khelwood
    Mar 8 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 8 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 8 at 15:10











  • A dictionary might be a better fit here: How do you create a dictionary in Java?. The "code" would be the key, and the "definition" would be the value.

    – Johnny Mopp
    Mar 8 at 15:10


















  • Do you understand what the exception means?

    – Thorbjørn Ravn Andersen
    Mar 8 at 15:06











  • Perhaps you might be able to reduce this to a Minimal, Complete, and Verifiable example.

    – khelwood
    Mar 8 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 8 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 8 at 15:10











  • A dictionary might be a better fit here: How do you create a dictionary in Java?. The "code" would be the key, and the "definition" would be the value.

    – Johnny Mopp
    Mar 8 at 15:10

















Do you understand what the exception means?

– Thorbjørn Ravn Andersen
Mar 8 at 15:06





Do you understand what the exception means?

– Thorbjørn Ravn Andersen
Mar 8 at 15:06













Perhaps you might be able to reduce this to a Minimal, Complete, and Verifiable example.

– khelwood
Mar 8 at 15:06





Perhaps you might be able to reduce this to a Minimal, Complete, and Verifiable example.

– khelwood
Mar 8 at 15:06




2




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 8 at 15:06





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 8 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 8 at 15:10





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 8 at 15:10













A dictionary might be a better fit here: How do you create a dictionary in Java?. The "code" would be the key, and the "definition" would be the value.

– Johnny Mopp
Mar 8 at 15:10






A dictionary might be a better fit here: How do you create a dictionary in Java?. The "code" would be the key, and the "definition" would be the value.

– Johnny Mopp
Mar 8 at 15:10













2 Answers
2






active

oldest

votes


















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.






share|improve this answer























  • How would i use array list i just imported it

    – Quinn
    Mar 8 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 at 6:29


















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];





share|improve this answer

























  • Hey Now it gives me this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Main.main(Main.java:100)

    – Quinn
    Mar 8 at 16:27











  • Did you changed also size of listedDefinition array?

    – Boken
    Mar 8 at 22:09

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer























  • How would i use array list i just imported it

    – Quinn
    Mar 8 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 at 6:29















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.






share|improve this answer























  • How would i use array list i just imported it

    – Quinn
    Mar 8 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 at 6:29













0












0








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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 15:17









Prerna GandhiPrerna Gandhi

11




11












  • How would i use array list i just imported it

    – Quinn
    Mar 8 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 at 6:29

















  • How would i use array list i just imported it

    – Quinn
    Mar 8 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 at 6:29
















How would i use array list i just imported it

– Quinn
Mar 8 at 16:53





How would i use array list i just imported it

– Quinn
Mar 8 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 at 6:29





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 at 6:29













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];





share|improve this answer

























  • Hey Now it gives me this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Main.main(Main.java:100)

    – Quinn
    Mar 8 at 16:27











  • Did you changed also size of listedDefinition array?

    – Boken
    Mar 8 at 22:09















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];





share|improve this answer

























  • Hey Now it gives me this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Main.main(Main.java:100)

    – Quinn
    Mar 8 at 16:27











  • Did you changed also size of listedDefinition array?

    – Boken
    Mar 8 at 22:09













0












0








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];





share|improve this answer















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];






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 16:17

























answered Mar 8 at 15:17









BokenBoken

1,6531119




1,6531119












  • Hey Now it gives me this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Main.main(Main.java:100)

    – Quinn
    Mar 8 at 16:27











  • Did you changed also size of listedDefinition array?

    – Boken
    Mar 8 at 22:09

















  • Hey Now it gives me this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Main.main(Main.java:100)

    – Quinn
    Mar 8 at 16:27











  • Did you changed also size of listedDefinition array?

    – Boken
    Mar 8 at 22:09
















Hey Now it gives me this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Main.main(Main.java:100)

– Quinn
Mar 8 at 16:27





Hey Now it gives me this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Main.main(Main.java:100)

– Quinn
Mar 8 at 16:27













Did you changed also size of listedDefinition array?

– Boken
Mar 8 at 22:09





Did you changed also size of listedDefinition array?

– Boken
Mar 8 at 22:09



Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved