Java Scanner does not wait for input

喜欢而已 提交于 2019-12-05 12:56:34

According to the java.util.Scanner javadoc, Scanner.close() closes the associated stream if this stream implements the Closeable interface. java.lang.System.in is an InputStream, which implements the Closeable interface. Hence, after calling Scanner.close() on a Scanner which is associated with System.in, the System.in stream is closed and not available anymore.

The following SSCCE works for me. I have removed some code from the question which is not relevant to the actual issue. Note that with this approach, while it works, Eclipse gives me the warning "Resource leak: 'scan' is never closed", so a better solution would be to use one Scanner instance only.

package com.example;

import java.util.Scanner;
import java.util.Set;
import static java.lang.System.out;

public class ScannerTest {
    int p = 0;
    String name = "Test";

    public void startGame() {
        out.println("Player1: 1 for dumb player, 2 for smart player, 3 for human player.");
        Scanner scan = new Scanner(System.in);
        p = scan.nextInt();

        out.println("Result1: " + p);

        out.println("Player2: 1 for dumb player, 2 for smart player, 3 for human player.");
        p = scan.nextInt();

        out.println("Result2: " + p);

        // scan.close();   // Do not close the Scanner to leave System.in open
    }

    public int findBestMove(Set<Integer> moves, Object /*Board*/ b) {
        out.println("Player " +name+ ", select a column from 1-7: ");
        Scanner scan = new Scanner(System.in);
        int move = scan.nextInt();
        // scan.close();   // Do not close the Scanner to leave System.in open

        out.println("Move: " + move);

        return 0;
    }

    public void run() {
        startGame();
        findBestMove(null, null);
    }

    public static void main(String[] args) {
        ScannerTest st = new ScannerTest();
        st.run();
    }
}

nextInt() does not discard \n in the stream, so the next call finds nothing. You have to skip it manually with Scanner.skip("\n") or Scanner.nextLine()

EDIT After figuring out that "skipping readInt()" meant that the code threw an exception. and with @Andreas help, here's an alternative solution. In java6 a new class Console has been added for providing a better interface to stdin. And it returns a Reader that doesn't care how much you close it. So the following snippet works just fine:

    Scanner fi = new Scanner(System.console().reader());
    System.out.println(fi.nextInt());
    fi.close();

    fi = new Scanner(System.console().reader());
    System.out.println(fi.nextInt());
    fi.close();

Try using scan.reset(); instead of scan.close(). scan.close() will throw NoSuchElementException as Denis Tulskiy said.

I just spent about 15 minutes troubleshooting this issue and my problem was different than all of the above.

The code is fine, but I am using Sublime Text and running it in the terminal emulator inside sublime text, on windows. This prevented the scanner from actually allowing me to try to input something.

I ran the app using cmd and it worked fine. I hope this helps someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!