Results 1 to 3 of 3
Thread: array problem
- 03-31-2010, 07:11 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
array problem
Hi i have a question on this problem ive been working on.
I am trying to get the program to read in Strings line by line and count the number of awards a person has.
the text the program is reading is:
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 everytime a comma appears the count goes up and when it meets a name(no comma) it resets back to 0 and should move to the next array butPHP Code:String str; int count = 0; int[] counts = new int[3]; BufferedReader reader = new BufferedReader( new StringReader(awardWinners.toString())); for(int y = 0; y< counts.length ; y++){ while((str = reader.readLine()) != null){ int x = str.indexOf(", "); if(x != -1) count++; else{ count = 0; } counts[y] = count; System.out.println(Arrays.toString(counts)); } }
when i run it, it prints out
i cant get the count to go to the next array.[0, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
[2, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
[2, 0, 0, 0, 0]
[3, 0, 0, 0, 0]
[4, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
[2, 0, 0, 0, 0]
[3, 0, 0, 0, 0]
what do you guys suggest?
thanks!
- 03-31-2010, 09:39 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
You're reading your entire file in the first pass of your outermost loop; get rid of that loop and 'manually' increment y:
kind regards,Java Code:int y= -1; // the number to increment while((str = reader.readLine()) != null){ // read the entire file int x = str.indexOf(", "); if(x != -1) counts[y]++; else{ y++; // next numbers to read counts[y] = 0; } System.out.println(Arrays.toString(counts)); }
Jos
- 03-31-2010, 09:54 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Array problem
By c3jcarmy in forum New To JavaReplies: 11Last Post: 03-11-2010, 02:45 AM -
Array problem
By binarzt in forum New To JavaReplies: 5Last Post: 02-14-2010, 09:01 AM -
array problem
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-05-2008, 02:25 AM -
array problem
By wats in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:08 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks