0

I have a txt file that contains 40 names. Each name is on its own line. This method should take each name and place it into an array of 4 elements and then take that array and write those files to another txt file with use of another method.

My issue is every forth name in the list somehow ends up being null and my output txt file ends up with 10 rows and a null as the forth element in each row.

I have provided code and sample I/O below. Thanks in advance!

Sample Input

Emily
Reba
Emma
Abigail
Jeannie
Isabella
Hannah
Samantha

My method

public static void fillArray(String[] player ,String[] team, BufferedReader br) throws IOException{
  String line;
  int count = 0;

  while((line = br.readLine()) != null){
    if(count < 3){
       player[count] = line;
       count++;
    }
    else{
       count = 0;
       writeFile(player);
    }
  }
  br.close();

}

Sample Output

Emily Reba Emma null 
Jeannie Isabella Hannah null 
4

1 回答 1

2

你的逻辑不正确。player[3]永远不会设置,并且下一个循环最终会读取一行而不将其存储到数组中。用这个:

public static void fillArray(String[] player ,String[] team, BufferedReader br) throws IOException{
  String line;
  int count = 0;

  while((line = br.readLine()) != null){
    player[count] = line;
    count++;
    if (count == 4) {
       count = 0;
       writeFile(player);
    }
  }
于 2013-02-14T03:23:59.900 回答