Share on Google+Share on Google+

How to read from the console

Advertisement
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:

How to read password from the console
This tutorial demonstrate how to read password without showing it on the console
 
How to read and display password from the console
This tutorial demonstrate how to read and display password from the console.
 

Advertisements

Advertisement

Posted on: April 10, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.