In Java, there are 3 ways to read input from a console :

  1. BufferedReader + InputStreamReader (Classic)
  2. Scanner (JDK 1.5)
  3. System.console (JDK 1.6)

1. BufferedReader + InputStreamReader

For experienced Java developers, you will miss this classic way to read a system input.

ReadConsole.java

package com.mkyong;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadConsole {

    public static void main(String[] args) {

        BufferedReader br = null;

        try {

            br = new BufferedReader(new InputStreamReader(System.in));

            while (true) {

                System.out.print("Enter something : ");
                String input = br.readLine();

                if ("q".equals(input)) {
                    System.out.println("Exit!");
                    System.exit(0);
                }

                System.out.println("input : " + input);
                System.out.println("-----------\n");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

Output

Enter something : old and classic
input : old and classic
-----------

Enter something : q
Exit!

2. Scanner

In JDK 1.5, the developer starts to use java.util.Scanner to read system input.

ReadConsole2.java

package com.mkyong;

public class ReadConsole2 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        while (true) {

            System.out.print("Enter something : ");
            String input = scanner.nextLine();

            if ("q".equals(input)) {
                System.out.println("Exit!");
                break;
            }

            System.out.println("input : " + input);
            System.out.println("-----------\n");
        }

        scanner.close();

    }

}

Output

Enter something : hello jdk 1.5
input : hello jdk 1.5
-----------

Enter something : scanner example
input : scanner example
-----------

Enter something : q
Exit!

3. System.console

In JDK 1.6, the developer starts to switch to the more simple and powerful java.io.Console class.

ReadConsole3.java

package com.mkyong;

public class ReadConsole3 {

    public static void main(String[] args) {

        while (true) {

            System.out.print("Enter something : ");
            String input = System.console().readLine();

            if ("q".equals(input)) {
                System.out.println("Exit!");
                System.exit(0);
            }

            System.out.println("input : " + input);
            System.out.println("-----------\n");
        }

    }

}

Output

Enter something : hello jdk 1.6
input : hello jdk 1.6
-----------

Enter something : console example
input : cosole example
-----------

Enter something : q
Exit!
Note
This System.console() is usable only outside IDE, a bit hard for testing.

References

  1. JavaDoc – java.io.Console
  2. JavaDoc – java.util.Scanner
  3. why do we use console class