Java Console.reader()

Syntax

Console.reader() has the following syntax.

public Reader reader()

Example

In the following code shows how to use Console.reader() method.


// ww  w .ja  v a 2 s. co m
import java.io.Console;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) throws Exception {

    Console cnsl = System.console();

    if (cnsl != null) {
      System.out.print("Enter name : ");
      Scanner scan = new Scanner(cnsl.reader());
      while (scan.hasNext()) {
        String str = scan.next();
        System.out.println(str);
      }
    }
  }
}

The code above generates the following result.