0

I have an instance of Scanner application-wide defined as Scanner scanner = new Scanner(System.in). When I call scanner.nextLine(), will it block if no line is found and wait for user input?

Strangely, if I run my code via IntelliJ, it will work properly and wait for user to input a line. However, when run in terminal, it will just throw java.util.NoSuchElementException: No line found on first call of scanner.nextLine(). What happened and how I can tune it?

A minimum sample

public class ScannerExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Input: ");
        String input = scanner.nextLine();
        System.out.println("Output: " + input);
    }
}

Run via IntelliJ

Run via Terminal

McXD
  • 27
  • 1
  • 8

0 Answers0