Java
User Input
Programming
Coding Tutorial
Software Development

How to get the user input in Java?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Java, a widely-used programming language, offers several mechanisms to capture user input, making it possible to build interactive applications. Understanding how to obtain user input is fundamental in Java, as it allows for dynamic responses and adaptive processes in programs. This article explains the primary methods and best practices for getting user input in Java.

Utilizing Scanner Class

The most common approach to handle user input in console-based Java applications is using the Scanner class from the java.util package. Scanner makes it easy to parse primitive types and strings using regular expressions.

How to Use Scanner:

  1. Import the Scanner Class
    You need to import the java.util.Scanner class at the beginning of your Java program.
java
   import java.util.Scanner;
  1. Create an Instance of Scanner
    You instantiate a Scanner object and pass the System.in input stream to it. This enables the Scanner to read input from the console.
java
   Scanner scanner = new Scanner(System.in);
  1. Read Input
    The Scanner class offers various methods to read different types of data. For example, nextLine() reads a string, nextInt() reads an integer, and nextDouble() reads a double value.
java
1   System.out.println("Enter your name: ");
2   String name = scanner.nextLine();
3   
4   System.out.println("Enter your age: ");
5   int age = scanner.nextInt();
  1. Close the Scanner
    It's a good practice to close the Scanner object after you are done using it to free up resources.
java
   scanner.close();

Methods of Scanner:

MethodDescription
next()Finds and returns the next complete token
nextLine()Advances the scanner past the current line and returns the input that was skipped
nextInt()Scans the next token as an int
nextDouble()Scans the next token as a double
close()Closes the scanner

Using BufferedReader and InputStreamReader

Another way to handle input in Java is by using BufferedReader combined with InputStreamReader. This combination is particularly useful when you need to handle larger inputs efficiently.

Steps to Use BufferedReader:

  1. Import Necessary Classes
    Just like Scanner, you first need to import the required classes from the java.io package.
java
   import java.io.BufferedReader;
   import java.io.InputStreamReader;
  1. Create BufferedReader Object
    You create BufferedReader using InputStreamReader which reads bytes from System.in and decodes them into characters.
java
   BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  1. Read Input
    BufferedReader provides the readLine() method to read a line of text.
java
   System.out.println("Enter your favorite quote: ");
   String quote = reader.readLine();
  1. Close the BufferedReader
    Similarly, it's important to close the BufferedReader after use.
java
   reader.close();

Conclusion and Best Practices

When deciding between Scanner and BufferedReader, consider the specific needs of your application in terms of efficiency and convenience. Scanner is more versatile for parsing different data types, while BufferedReader can be more efficient with large volumes of data.

Key Use-Cases for User Input in Java Programs:

  • Interactive CLI (Command Line Interface) applications
  • Data driven applications that require user settings or configurations
  • Educational programs for coding practice and tests
  • Simple games that run on the console

Understanding how to manage user input efficiently is essential for developing interactive and flexible Java programs. With this skill, you can undertake a wide array of programming tasks and build applications that significantly interact with users.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.