How to Get a Keyboard Input in Java

Mohammad Irfan Feb 02, 2024
  1. Getting Keyboard Input Using BufferedReader in Java
  2. Getting Keyboard Input Using Scanner Class in Java
  3. Getting Keyboard Input Using Console Class in Java
  4. Getting Keyboard Input Using command-line arguments in Java
How to Get a Keyboard Input in Java

This tutorial introduces how to get a keyboard input or user input in Java. We also included example programs to help you understand this topic better.

To get a user input in Java, you’ll encounter several classes such as the Scanner, BufferedReader, and Console. We’ll use these classes for our operation as we show you the different methods you can follow.

Getting Keyboard Input Using BufferedReader in Java

In this example, we used the BufferedReader class that takes the InputStreamReader class’s instance to read a user input. Here, the readLine() method reads the user input and returns a string as a result:

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class SimpleTesting {
  public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter Input : ");
    try {
      String s = br.readLine();
      System.out.println(s);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

Output:

Enter Input : 
25
25

Getting Keyboard Input Using Scanner Class in Java

The Scanner class is one of the simplest ways to get user input in Java. This class provides several built-in methods to get the input of various types like int and float. Here, we used the nextInt() method to get the int type of the input:

import java.util.Scanner;
public class SimpleTesting {
  public static void main(String[] args) {
    System.out.println("Enter Input : ");
    Scanner scanner = new Scanner(System.in);
    int a = scanner.nextInt();
    System.out.println(a);
    scanner.close();
  }
}

Output:

Enter Input :
25
25

Getting Keyboard Input Using Console Class in Java

We can use the Console class to get user input in Java. This class belongs to the java.io package and provides the readLine() method to read user input from the console. See the example below:

import java.io.Console;
public class Main {
  public static void main(String[] args) {
    Console console = System.console();
    String str = console.readLine("Enter Input : ");
    System.out.println(str);
  }
}

Output:

Enter Input : 
25
25

Getting Keyboard Input Using command-line arguments in Java

In Java, command-line arguments are the arguments that are passed during program execution. The main() method of the program has a string-type parameter that holds the input supplied from the terminal. Remember to get the command-line argument to pass them during program execution and that the type of input is always a string type.

public class SimpleTesting {
  public static void main(String[] args) {
    if (args.length > 0) {
      System.out.println("User Input");
      for (int i = 0; i < args.length; i++) {
        System.out.println(args[i]);
      }
    } else
      System.out.println("No Input Found");
  }
}

Run the code and pass the keyboard input as command-line arguments.

java SimpleTesting Hello DelftStack

Output:

User Input
Hello
DelftStack

Related Article - Java Scanner

Related Article - Java Input