Java Console Input

From KevinWiki

(Difference between revisions)
Jump to: navigation, search
(New page: Category:Java <source lang="java5"> package kevin.console.test; import java.io.BufferedReader; import java.io.Console; import java.io.IOException; import java.io.InputStreamReader; i...)
Line 2: Line 2:
<source lang="java5">
<source lang="java5">
-
package kevin.console.test;
+
package com.lckymn.kevin.console.test;
import java.io.BufferedReader;
import java.io.BufferedReader;
Line 17: Line 17:
{
{
-
/**
+
    /**
-
* @param args
+
    * @param args
-
* @throws IOException
+
    * @throws IOException
-
*/
+
    */
-
public static void main(String[] args)
+
    public static void main(String[] args)
-
{
+
    {
-
try
+
        try
-
{
+
        {
-
String line = null;
+
            String line = null;
-
/* J2SE 1.4 */
+
            /* J2SE 1.4 */
-
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
+
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
-
System.out.print("Please enter your ID: ");
+
            System.out.print("Please enter your ID: ");
-
line = bufferedReader.readLine();
+
            line = bufferedReader.readLine();
-
System.out.println(line);
+
            System.out.println(line);
-
/* Java 5 */
+
            /* Java 5 */
-
Scanner scanner = new Scanner(System.in);
+
            Scanner scanner = new Scanner(System.in);
-
System.out.print("Please enter your ID: ");
+
            System.out.print("Please enter your ID: ");
-
line = scanner.nextLine();
+
            line = scanner.nextLine();
-
System.out.println(line);
+
            System.out.println(line);
-
/* Java 6 */
+
            /* Java 6 */
-
Console console = System.console(); // It does not work on Eclipse (it always returns null).
+
            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.readLine("Please enter your ID: "));
-
System.out.println(console.readPassword("Please enter your password: "));
+
            System.out.println(console.readPassword("Please enter your password: "));
-
}
+
        }
-
catch (Exception e)
+
        catch (Exception e)
-
{
+
        {
-
e.printStackTrace();
+
            e.printStackTrace();
-
}
+
        }
-
finally
+
        finally
-
{
+
        {
-
System.exit(0);
+
            System.exit(0);
-
}
+
        }
-
}
+
    }
}
}
</source>
</source>

Revision as of 13:36, 29 September 2008


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
        {
            String line = null;
            /* J2SE 1.4 */
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Please enter your ID: ");
            line = bufferedReader.readLine();
            System.out.println(line);
 
            /* Java 5 */
            Scanner scanner = new Scanner(System.in);
            System.out.print("Please enter your ID: ");
            line = scanner.nextLine();
            System.out.println(line);
 
            /* 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