Question: I want to sequentially read each line of an input unsorted file into consecutive elements of the array until there are no more records in
the file or until the input size is reached, whichever occurs first. but i can't think of a way to check the next line if its the end of the file?

This is my code:

```
 Scanner cin = new Scanner(System.in);
    System.out.print("Max number of items: ");
    int max = cin.nextInt();

    String[] input = new String[max];

    try {
        BufferedReader br = new BufferedReader(new FileReader("src/ioc.txt"));
        for(int i=0; i<max; i++){ //to do:check for empty record
            input[i] = br.readLine();
        }
    }
    catch (IOException e){
        System.out.print(e.getMessage());
    }

    for(int i=0; i<input.length; i++){
        System.out.println((i+1)+" "+input[i]);
    }

```

the file has 205 lines, if I input 210 as max, the array prints with five null elements like so..

```
..204 Seychelles
205 Algeria
206 null
207 null
208 null
209 null
210 null

```

Thanks for your responses in advance!

Answer: 
From [the docs](http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html): 

> 
> public String readLine() 
> 
> 
> Returns: A String containing the contents of the line, not including
>  any line-termination characters, or null if the end of the stream has
>  been reached
> 
> 
> 

In other words, you should do

```
String aux = br.readLine();
if(aux == null)
   break;
input.add(aux)

```

I recomend you use a variable-size array (you can pre-allocated with the requested size if reasonable). Such that you get either the expected size or the actual number of lines, and can check later.

(depending on how long your file is, you might want to look at [readAllLines()](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines(java.nio.file.Path,%20java.nio.charset.Charset)) too.)
Use `List<String>`

```
List<String> lines = new ArrayList<>(); // Growing array.
try (BufferedReader br = new BufferedReader(new FileReader("src/ioc.txt"))) {
    for(;;) {
        String line = br.readLine();
        if (line == null) {
            break;
        }
        lines.add(line);
    }
} catch (IOException e) {
    System.out.print(e.getMessage());
} // Closes automatically.

// If lines wanted as array:
String[] input = lines.toArray(new String[lines.size()]);

```

Using a dynamically growing ArrayList is the normal way to deal with such problem.

P.S.

FileReader will read in the current platform encoding, i.e. a local file, created locally.