Java - How to read input from console

Posted on November 14, 2016


In java, we can read input from console or keyboard by using

  • java.util.Scanner  class
  • java.io.BufferedReader class
  • java.io.Console class

Read input from console using java.util.Scanner

Here is an example of reading input from console using the Scanner class.

ConsoleInputExample1.java

package com.boraji.tutorial.io;

import java.util.Scanner;

/**
 * @author imssbora
 */
public class ConsoleInputExample1 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		// Read String
		System.out.println("Enter your name.");
		String name = scanner.nextLine();
		System.out.println("Your name is : " + name);

		// Read Int
		System.out.println("Enter your age.");
		int age = scanner.nextInt();
		System.out.println("Your age is : " + age);

		scanner.close();
	}
}

Output

Enter your name.
Sunil Singh Bora
Your name is : Sunil Singh Bora
Enter your age.
30
Your age is : 30

 

Read input from console using java.io.BufferedReader

The following example read input string from console using readLine() method of the BufferedReader class.

ConsoleInputExample2.java

package com.boraji.tutorial.io;

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

/**
 * @author imssbora
 */
public class ConsoleInputExample2 {
	public static void main(String[] args) {
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(System.in));

			// Read first line
			System.out.println("Enter first line");
			String line = bufferedReader.readLine();
			System.out.println("First line is : " + line);

			// Read second line
			System.out.println("\nEnter second line");
			line = bufferedReader.readLine();
			System.out.println("Second line is : " + line);

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

	}
}

Output

Enter first line
This is an example of reading input
First line is : This is an example of reading input

Enter second line
from console using buffered reader.
Second line is : from console using buffered reader.

 

Read input using java.io.Console

The following example will not work on IDE. You can run this example from window command prompt.

package com.boraji.tutorial.io;

import java.io.Console;

/**
 * @author imssbora
 */
public class ConsoleInputExample3 {
	public static void main(String[] args) {
		Console console = System.console();

		if (console != null) {
			// Read String
			System.out.println("Enter username: ");
			String name = console.readLine();
			System.out.println("Username is : " + name);

			// Read password
			System.out.println("Enter your password.");
			char[] password = console.readPassword();
			System.out.println("Password is : " + new String(password));
		}

	}
}

CMD output

Enter username:
imssbora
Username is : imssbora
Enter your password.

Password is : [email protected]