Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Need help with Java homework

Reply
Thread Tools

Need help with Java homework

 
 
bd
Guest
Posts: n/a
 
      11-07-2006
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

 
Reply With Quote
 
 
 
 
Lionel
Guest
Posts: n/a
 
      11-07-2006
bd wrote:
> 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.
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      11-07-2006

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

 
Reply With Quote
 
bd
Guest
Posts: n/a
 
      11-08-2006
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!

 
Reply With Quote
 
Lars Enderin
Guest
Posts: n/a
 
      11-08-2006
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.
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      11-08-2006

Lars Enderin wrote:
> citizenCount[votingDistrict-1]++;
> is even better.


One lesson at a time Lars

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-12-2006
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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      11-12-2006

Lew wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: I need help with my homework!! Emergency!!! Karl Heinz Buchegger C++ 1 11-23-2004 05:06 PM
Re: I need help with my homework!! Emergency!!! Mike C++ 1 11-23-2004 04:05 AM
Need some help for some perl homework.... Perl 0 02-25-2004 01:45 AM
need help with homework Rhino Java 1 11-16-2003 06:52 PM
Need Help On Homework Allens Mail C++ 1 07-14-2003 05:52 AM



Advertisments