Java Console Input

From KevinWiki

Revision as of 12:39, 6 October 2008 by Kevin (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


package com.lckymn.kevin.console.test;
 
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
 
/**
 * @author Lee, SeongHyun (Kevin)
 * 
 */
public class ConsoleInputTest
{
 
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args)
    {
        try
        {
            /* J2SE 1.4 */
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Please enter your ID: ");
            System.out.println(bufferedReader.readLine());
 
            /* Java 5 */
            Scanner scanner = new Scanner(System.in);
            System.out.print("Please enter your ID: ");
            System.out.println(scanner.nextLine());
 
            /* Java 6 */
            Console console = System.console(); // It does not work on Eclipse (it always returns null).
            System.out.println(console.readLine("Please enter your ID: "));
            System.out.println(console.readPassword("Please enter your password: "));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            System.exit(0);
        }
    }
 
}
Personal tools