Need help with Java homework

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
 
L

Lionel

bd said:
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.

Array indexing starts at 0 so the possible indexes are 0 to 21. When you
try to add something at 22 you are actually trying to add a 23rd element
of which you haven't allocated space for.

Lionel.
 
D

Daniel Pitts

bd said:
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

You are so close to a correct program. The problem is indexing..
When you have an int[] myArray = new int[15];
myArray is now an array with 15 elements, numbered 0 through 14.
myArray[0] // first element
myArray[14] // last element
myArray[15] // java.lang.ArrayIndexOutOfBoundsException: 15

We call this an off-by-one error.
it looks to me like votingDistrict might be a number in the range
[1,22] inclusive. If that is the case, then you need to use
votingDistrict - 1 as the index. Or, have votingDistrict be a number
in the range [0, 21] inclusive.

Hope this helps.
 
B

bd

AWESOME -

Changing:

citizenCount[votingDistrict] = citizenCount[votingDistrict] + 1;

to

citizenCount[votingDistrict-1] = citizenCount[votingDistrict-1] + 1;

cleared up the problem.

I understand what I was doing wrong now. I was trying to call a part
of the array that didn't exist.

THANKS!
 
L

Lars Enderin

bd skrev:
AWESOME -

Changing:

citizenCount[votingDistrict] = citizenCount[votingDistrict] + 1;

to

citizenCount[votingDistrict-1] = citizenCount[votingDistrict-1] + 1;

cleared up the problem.
citizenCount[votingDistrict-1]++;
is even better.
 
L

Lew

This was the *right* way to ask for homework help.

The subject line identifies that it's for homework help.

The body included evidence that the OP had tried to solve the problem, stated
clearly the specific problem with the tentative solution, and asked for help
understanding the issue rather than for crib notes or a complete solution.

It left open the availability (demonstrated in the responses) for answers to
teach rather than to spoon-feed.

It exemplifies for other students how you *can* get help with homework on the
newsgroups.

Good going, bd.

- Lew
 
D

Daniel Pitts

Lew said:
This was the *right* way to ask for homework help.

The subject line identifies that it's for homework help.

The body included evidence that the OP had tried to solve the problem, stated
clearly the specific problem with the tentative solution, and asked for help
understanding the issue rather than for crib notes or a complete solution.

It left open the availability (demonstrated in the responses) for answers to
teach rather than to spoon-feed.

It exemplifies for other students how you *can* get help with homework on the
newsgroups.

Good going, bd.

- Lew

I agree, far too many people ask for the answer, rather than the
understanding. bd seems truely interested in learning. :) Good luck
BD.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top