How to read from the console


 

How to read from the console

The tutorial demonstrate the use of Scanner Class.

The tutorial demonstrate the use of Scanner Class.

Description:

Scanner was introduced in jdk 1.5. This class help in taking the input from the console. Here in this sample program it will take one word input from the console and display it.

Code:

import java.util.Scanner;
import java.io.Console;

public class ConsoleExample {
  public static void main(String args[]) {
    Console cons;
    cons = System.console();
    Scanner sc = new Scanner(cons.reader());
    System.out.println(sc.next());
  }
}

Output:

Ads