Results 1 to 10 of 10
Thread: array going out of bounds?
- 03-31-2010, 09:50 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
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, 2009the 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.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));
im not sure why its going out of bounds. thanks!Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at MainDriver.main(MainDriver.java:157)
- 03-31-2010, 10:22 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 04-01-2010, 12:36 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
hmm i dont think so
- 04-01-2010, 12:43 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-01-2010, 12:54 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
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
how would i make it so the next name would occupy the next blank array?[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]Last edited by jabo; 04-01-2010 at 01:00 AM.
- 04-01-2010, 01:34 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
-
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).
- 04-01-2010, 01:50 AM #8
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
thank you guys
i fixed it by adding brackets around the loop
- 04-02-2010, 12:26 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Great! Braces are good.
- 04-02-2010, 10:08 AM #10
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
array Index out of Bounds exception== 0
By Allgorythm in forum New To JavaReplies: 6Last Post: 02-11-2010, 04:02 PM -
Array Index Out of Bounds Exception
By kool001 in forum New To JavaReplies: 1Last Post: 12-03-2009, 07:42 AM -
Array out of bounds exception 20.
By dropt in forum New To JavaReplies: 4Last Post: 09-21-2009, 10:32 PM -
Array Index out of bounds
By leapinlizard in forum New To JavaReplies: 5Last Post: 04-29-2009, 05:11 AM -
why is my array out of bounds?
By Phobos0001 in forum New To JavaReplies: 3Last Post: 03-24-2008, 01:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks