Why won't the scanner prompt me for an input?

I have this method:

public String scanInput()
{
    String input = "";
    Scanner skanner = new Scanner(System.in);
    while(skanner.hasNextLine()){
        input = skanner.nextLine();
    }
    skanner.close();
    return input;
}

The first time this method is run, the program stops, and I'm prompted for an input in the console before it can proceed. However, the second time the method is run, it flashes by without pausing, and my program is stuck in an endless loop.

What line in this method makes the program pause, and why does it only pause the first time?

Jon Skeet
people
quotationmark

You're closing the scanner near the end of the method. That closes the stream it's working on - System.in in this case. That means next time you call the method, the stream is still closed, so there are no lines.

You should set up one Scanner, and pass that into the method (or have it as state within your class).

people

See more on this question at Stackoverflow