Need help with Java homework

Discussion in 'Java' started by bd, Nov 7, 2006.

  1. bd

    bd Guest

    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
     
    bd, Nov 7, 2006
    #1
    1. Advertising

  2. bd

    Lionel Guest

    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.
     
    Lionel, Nov 7, 2006
    #2
    1. Advertising

  3. bd

    Daniel Pitts Guest

    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.
     
    Daniel Pitts, Nov 7, 2006
    #3
  4. bd

    bd Guest

    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!
     
    bd, Nov 8, 2006
    #4
  5. bd

    Lars Enderin Guest

    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.
     
    Lars Enderin, Nov 8, 2006
    #5
  6. bd

    Daniel Pitts Guest

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


    One lesson at a time Lars :)
     
    Daniel Pitts, Nov 8, 2006
    #6
  7. bd

    Lew Guest

    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
     
    Lew, Nov 12, 2006
    #7
  8. bd

    Daniel Pitts Guest

    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.
     
    Daniel Pitts, Nov 12, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    709
    Guest
    Feb 25, 2004
  2. Rhino

    need help with homework

    Rhino, Nov 16, 2003, in forum: Java
    Replies:
    1
    Views:
    321
    Roedy Green
    Nov 16, 2003
  3. Allens Mail

    Need Help On Homework

    Allens Mail, Jul 13, 2003, in forum: C++
    Replies:
    1
    Views:
    387
    Aggro
    Jul 14, 2003
  4. Mike
    Replies:
    1
    Views:
    342
    Phlip
    Nov 23, 2004
  5. Karl Heinz Buchegger

    Re: I need help with my homework!! Emergency!!!

    Karl Heinz Buchegger, Nov 23, 2004, in forum: C++
    Replies:
    1
    Views:
    401
    Raymond Martineau
    Nov 23, 2004
Loading...

Share This Page