B
bd
I am learning about arrays this week in programming class.
How come in the following code below (of which alot has been removed) I
can't store anything in the citizenCount array?
<-snipped->
//Declare variables
String votingDistrictString; // String version of voting district.
int votingDistrict = 0; // Citizen's votingDistrict.
int citizenCount[] = new int[22]; // Array of counts by voting
district.
<-snipped->
//Get Voting District
votingDistrictString = br.readLine();
votingDistrict = Integer.parseInt(votingDistrictString);
//Add 1 to the value for the voting district in the array
citizenCount[votingDistrict] = citizenCount[votingDistrict] + 1;
When I execute my program, I get this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 22
at Census.main(Census.java:40)
The 40th line is the "citizenCount[votingDistrict] =
citizenCount[votingDistrict] + 1;" line so I know I'm doing something
wrong when I try to store something in the array.
I can post the entire code if necessary.
Thanks,
Dale
How come in the following code below (of which alot has been removed) I
can't store anything in the citizenCount array?
<-snipped->
//Declare variables
String votingDistrictString; // String version of voting district.
int votingDistrict = 0; // Citizen's votingDistrict.
int citizenCount[] = new int[22]; // Array of counts by voting
district.
<-snipped->
//Get Voting District
votingDistrictString = br.readLine();
votingDistrict = Integer.parseInt(votingDistrictString);
//Add 1 to the value for the voting district in the array
citizenCount[votingDistrict] = citizenCount[votingDistrict] + 1;
When I execute my program, I get this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 22
at Census.main(Census.java:40)
The 40th line is the "citizenCount[votingDistrict] =
citizenCount[votingDistrict] + 1;" line so I know I'm doing something
wrong when I try to store something in the array.
I can post the entire code if necessary.
Thanks,
Dale