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: 
Please refer this [Number of lines in a file in Java](https://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java) and modify your for loop to take whatever is the least out of the entered max value or the no.of lines in the file.
```
    int i=0;
    for(; i<max; i++){ //to do:check for empty record
        String line=br.readLine();
        if(line==null){
            break;
        }
        input[i] = line;
    }
    //i will contain the count of lines read. indexes 0...(i-1) represent the data.

```