Results 1 to 10 of 10
  1. #1
    jabo is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default array going out of bounds?

    This is a similar question i had earlier,
    I am trying to fill in the array of strings with the name of the award winners however i am having trouble with the loop.
    here is the set of strings being read
    Bob Johnson
    Peace Prize, 1989
    Purple Heart, 1954
    Bryce Webb
    Silver Medal, 2009
    Heisman, 1963
    Grammy Award, 2010
    Academy Award, 2008
    Casey Hollingsworth
    Final Four, 2007
    Gold Medal, 2009
    Bronze Medal, 2009
    PHP Code:
    String str;
    	    String[] counts = new String[3];
    	    BufferedReader reader = new BufferedReader(
    	              new StringReader(awardWinners.toString()));
    	    int y= 0;
    	    
    			while((str = reader.readLine()) != null){
    			  int x = str.indexOf(", ");
    			  if(x == -1)
    				counts[y] = str;
    /*linen157*/		y++;
    
    			}
    		System.out.println(Arrays.toString(counts));
    the code creates array of 3 integers and im trying to set the line without commas as the value for each array but i get this error.
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    at MainDriver.main(MainDriver.java:157)
    im not sure why its going out of bounds. thanks!

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Do you possibly have any blank lines in your String? Try a System.out.println(str) just before you add str to the array.

    -Gary-

  3. #3
    jabo is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    hmm i dont think so

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    counts is quite a small array (length is 3). But you increment the y variable every time you read a line. If you hit a line without a comma anywhere from the fourth line onwards y will be >=3 and you will be out of bounds.

  5. #5
    jabo is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    haha yea 3 is kinda small, huh? im trying to get it to work with 50 arrays. 3 was just to test it.


    edit: ok i increased my array to 50 and heres what i got
    [Bob Johnson, null, null, Bryce Webb, null, null, null, null, Casey Hollingsworth, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
    how would i make it so the next name would occupy the next blank array?
    Last edited by jabo; 04-01-2010 at 02:00 AM.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Yeah, the problem isn't that the array is too small. Format your code properly and take another good look at it. You already got the answer from pbrockway2, but you are still not seeing it. Fix your indentation, and the answer should reveal itself.

    -Gary-

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You might want to scrap the idea of using arrays and instead create a couple of classes here, one perhaps called Award that holds a String for the award name and an int for the year, another, perhaps called AwardWinner that holds a String for the name of the person and an ArrayList<Award> for the multiple awards they've won, and then create an ArrayList<AwardWinner> to hold a collection of all the award winners.

    Next you'll have to figure out a way to tell as you're reading the file when a new person's name begins and read in the input accordingly. Perhaps you can check if the line contains a comma, and if not it's a name (of course you'll have problems if you run into any names like John Smith, Jr).

  8. #8
    jabo is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    thank you guys
    i fixed it by adding brackets around the loop

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Great! Braces are good.

  10. #10
    wangwei is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    Maybe u should include the code at [CODE] block, or it wraps unexpectedly.
    ^_^

Similar Threads

  1. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 05:02 PM
  2. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 08:42 AM
  3. Array out of bounds exception 20.
    By dropt in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 11:32 PM
  4. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 06:11 AM
  5. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 02:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •