Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Need help with Java homework

Thread view: 
bd - 07 Nov 2006 23:07 GMT
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
Lionel - 07 Nov 2006 23:11 GMT
> I am learning about arrays this week in programming class.
>
[quoted text clipped - 23 lines]
> 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.
Daniel Pitts - 07 Nov 2006 23:13 GMT
> I am learning about arrays this week in programming class.
>
[quoted text clipped - 28 lines]
> 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.
bd - 08 Nov 2006 16:43 GMT
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!
Lars Enderin - 08 Nov 2006 16:56 GMT
bd skrev:
> AWESOME -
>
[quoted text clipped - 7 lines]
>
> cleared up the problem.

citizenCount[votingDistrict-1]++;
is even better.
Daniel Pitts - 08 Nov 2006 18:43 GMT
> citizenCount[votingDistrict-1]++;
> is even better.

One lesson at a time Lars :-)
Lew - 12 Nov 2006 16:30 GMT
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
Daniel Pitts - 12 Nov 2006 18:32 GMT
> This was the *right* way to ask for homework help.
>
[quoted text clipped - 13 lines]
>
> - Lew

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.