In this article, we discuss three different ways for reading input from the user in the command line environment (also known as the “console”). Each way is fairly easy to use and also has its own advantages and drawbacks.

 

1. Reading User's Input using BufferedReader class

By wrapping the System.in (standard input stream) in an InputStreamReader which is wrapped in a BufferedReader, we can read input from the user in the command line. Here’s an example:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");

String name = reader.readLine();
System.out.println("Your name is: " + name);
In the above example, the readLine() method reads a line of text from the command line.

Advantages: The input is buffered for efficient reading.

Drawbacks: The wrapping code is hard to remember.

 

2. Reading User's Input using Scanner class

The main purpose of the Scanner class (available since Java 1.5) is to parse primitive types and strings using regular expressions, however it is also can be used to read input from the user in the command line. Here’s an example:

Scanner scanner = new Scanner(System.in);
System.out.print("Enter your nationality: ");
String nationality = scanner.nextLine();

System.out.print("Enter your age: ");
int age = scanner.nextInt();
Advantages:

  • Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the tokenized input.
  • Regular expressions can be used to find tokens.


Drawbacks:

  • The reading methods are not synchronized.
Learn more: Java Scanner Tutorial and Code Examples

 

3. Reading User's Input using Console class

The Console class was introduced in Java 1.6, and it has been becoming a preferred way for reading user’s input from the command line. In addition, it can be used for reading password-like input without echoing the characters entered by the user; the format string syntax can also be used (like System.out.printf()). Here’s an example code snippet:

Console console = System.console();
if (console == null) {
	System.out.println("No console: non-interactive mode!");
	System.exit(0);
}

System.out.print("Enter your username: ");
String username = console.readLine();

System.out.print("Enter your password: ");
char[] password = console.readPassword();

String passport = console.readLine("Enter your %d (th) passport number: ", 2);
Advantages:

  • Reading password without echoing the entered characters.
  • Reading methods are synchronized.
  • Format string syntax can be used.
Drawbacks:

  • Does not work in non-interactive environment (such as in an IDE).
Learn more: Java Console Input Output Examples

 

4. Java Reading User's Input Example Program

For your convenient and reference purpose, we combine the above code snippet into a demo program whose source code looks like this:

package net.codejava.io;

import java.io.*;
import java.util.*;

public class UserInputConsoleDemo {

	public static void main(String[] args) {

		// using InputStreamReader
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));			
			System.out.print("Enter your name: ");

			String name = reader.readLine();
			System.out.println("Your name is: " + name);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}

		// using Scanner
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter your nationality: ");
		String nationality = scanner.nextLine();
		System.out.println("Your nationality is: " + nationality);

		// using Console
		Console console = System.console();
		if (console == null) {
			System.out.println("No console: not in interactive mode!");
			System.exit(0);
		}

		System.out.print("Enter your username: ");
		String username = console.readLine();
		
		System.out.print("Enter your password: ");
		char[] password = console.readPassword();

		System.out.println("Thank you!");
		System.out.println("Your username is: " + username);
		System.out.println("Your password is: " + String.valueOf(password));

		// using Console with formatted prompt
		String job = console.readLine("Enter your job: ");
		
		String passport = console.readLine("Enter your %d (th) passport number: ", 2);

		System.out.println("Your job is: " + job);
		System.out.println("Your passport number is: " + passport);
	}	
}
 

Watch the video:

 

References:

 

Related Tutorials:

 

Other Java File IO Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (UserInputConsoleDemo.zip)UserInputConsoleDemo.zip[Java source file]1 kB

Add comment

   


Comments 

#3asma2020-09-23 04:28
how to solve this (Write a program that reads two names from the user. The Program should print to the
console wither the name are the same or not.
Quote
#2Dovid2020-09-01 08:49
Thank you for pointing out that the Console class does not work in a non-interactive environment. Explained why I didn't see it appearing.
Quote
#1Darnell2017-04-11 11:37
I'm trying to write a program that prompts the user to input a text file. Then ask the user for a lower-case letter to which the program will count the occurrence of the letter and also the total numbers of it's lower-case and upper-case occurrences. The program should prompt the user to "Enter name of the input file" and the program is supposed to find that text file. I've tried reading my book and searching through many tutorials to find the answer on how to create a program that can do this.
Quote