Question: I want to read in the 1st, 4th, 7th, etc. (every 3 lines) from a text file but am not sure how to go about doing so as nextLine() reads everything sequentially. Thank you for your suggestions?

```
Scanner in2 = new Scanner(new File("url.txt"));

while (in2.hasNextLine()) {
    // Need some condition here
    String filesURL = in2.nextLine();
}

```

Answer: 
Use a counter, and the `%` (modulus) operator so only every third line is read.

```
Scanner in = new Scanner(new File("url.txt"));

int i = 1;

while (in.hasNextLine()) {
    // Read the line first
    String filesURL = in.nextLine();

    /*
     * 1 divided by 3 gives a remainder of 1
     * 2 divided by 3 gives a remainder of 2
     * 3 divided by 3 gives a remainder of 0
     * 4 divided by 3 gives a remainder of 1
     * and so on...
     * 
     * i++ here just ensures i goes up by 1 every time this chunk of code runs.
     */
    if (i++ % 3 == 1) {
        // On every third line, do stuff; here I just print it
        System.out.println(filesURL);
    }
}

```
If you don't already have an index that tells you the file offset where each line begins in the file, then the only way to find each line is to read the file sequentially.

Are you sure the objective is not just to /output/ the 1st, 4th, 7th, etc lines? You can read all lines sequentially but keep only the ones you're interested in.