java reading user input

In this article I will shorty explain a few techniques how to read user input in Java.

Using Console

Reading user input from console with Console is very simple.

To request user input and read user entered line, use readLine method.
For reading passwords use readPassword method which hides entered data from the screen.

Example code:

import java.io.Console;

public class UsingConsole {
        public static void main(String[] args) {
                Console csl = System.console();
                String username = csl.readLine("Enter your username: ");
                char[] password = csl.readPassword("Enter password: ");

                System.out.println("Welcome, " + username +"!");
                System.out.print("Your password is: ");
                System.out.println(password);
        }
}

This program reads user username and password and then displays this information on the screen.

Example output:

Enter your username: Adam Scheller
Enter password:
Welcome, Adam Scheller! Your password is: secret

Note that I put printing of password to a separate line instead of using + operator like in username case. The reason behind this is that password is stored as array of char type. If I would try to use a + operator on it together with a String, Java would try to convert the char array into a String using toString() method. In such case we wouldn’t get our password as a String, but reference to the char array as a String.

To improve security of your code, it is a good idea to remove the password from computer memory after it is no longer used. You can do this by filling the password array with any value. For this reason readPassword method returns a char array instead of a String, because String type is immutable – once it is created, it cannot be modified.

Example way to remove the password from memory:

java.util.Arrays.fill(password,'x');

Important: to use the Console method in your code, you must run your code in a real console (ie. if you use Ubuntu, use Terminal; Windows user can use cmd.exe (command line)). Trying to utilize Console by running code in IDE (like Eclipse or Netbeans) will mostly result in failure due to lack of a real console.

Before using the console, it may be a good idea to check it against null:

Console.csl = System.console();
if(csl == null) { System.out.println("Console is not available!"); }

 

Using Scanner

Scanner class provides a set of useful methods for parsing input data. It can be used not just to parse input data from entered with a keyboard, but also from other sources like files.

To use Scanner to read user input from keyboard, we have to instantiate it with the standard input stream provided by java.lang.System.

Here is an example code that reads user name and age, and displays this information on the screen:

import java.util.Scanner;

public class UsingScanner {
   public static void main(String[] args) {
      Scanner scn = new Scanner(System.in);

      String name;
      int age;

      System.out.print("Enter your name: ");
      name = scn.nextLine();

      System.out.print("Enter your age: ");
      age = scn.nextInt();

      System.out.println("Your name is "+name+" and you are "+age+" years old.");
   }
}

Scanner contains also other methods for parsing and returning various data types, like nextDouble() or nextBigInteger(). Consult the documentation to learn more.

 

Using BufferedReader

BufferedReader, together with InputStreamReader, can be used for reading user input.

Example code:

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

public class UsingBufferedReader {
   public static void main(String[] args) {
      String name;
      int age;

      try(
         InputStreamReader isr = new InputStreamReader(System.in);
         BufferedReader br = new BufferedReader(isr);
      )
      {
         System.out.print("Enter your name: ");
         name = br.readLine();

         System.out.print("Enter your age: ");
         age = Integer.parseInt(br.readLine());

         System.out.println("Your name is "+name+" and you are "+age+" years old.");
      } catch (IOException e) {
         System.out.println(e.getMessage());
      }
   }
}

Learn more about Java