Q:

Scanner Class hasNextLine infinite loop

why does this piece of code go into an infinite loop when I try to give it a basic text file?
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class TestFile
{

    public static void main(String args[]) throws IOException
    {
            // Read in input file                                                                                                                                            
            File input = new File(args[0]);
            Scanner freader = new Scanner(input);
            while (freader.hasNextLine()) {
                System.out.println("hi");
            }

            freader.close();

    }

}

The print line just keeps going.

A:

Because hasNexLine() does neither get the line nor change the state of the scanner. if it's true once, and no other methods of the scanner are called, it'll always be true.

A:

Because you have to consume the nextLine so the code should be: 
while ( theScanner.hasNextLine() ) {
    String theLine = theScanner.nextLine();
}

If you don't invoke nextLine() you will always be watching at the same line and it will always answer true to that. 

A:

Add a call to nextLine or any other Scanner method that'll read in some input inside the while loop.
At the moment you're just repeatedly calling hasNextLine (which only returns a boolean, it doesn't modify the stream) without retrieving any input from freader, so if freader initially has another line within its input hasNextLine will always return true and your loop is essentially while (true).

