Results 1 to 3 of 3

Thread: array problem

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

    Default 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, 2009
    PHP 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));
    
        		}
        	}
    the 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 but
    when i run it, it prints out
    [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]
    i cant get the count to go to the next array.
    what do you guys suggest?
    thanks!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    You're reading your entire file in the first pass of your outermost loop; get rid of that loop and 'manually' increment y:

    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));
    }
    kind regards,

    Jos

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

Similar Threads

  1. Array problem
    By c3jcarmy in forum New To Java
    Replies: 11
    Last Post: 03-11-2010, 02:45 AM
  2. Array problem
    By binarzt in forum New To Java
    Replies: 5
    Last Post: 02-14-2010, 09:01 AM
  3. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 02:25 AM
  4. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:08 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 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
  •