Open In App

Ways to Read Input from Console in Java

Last Updated : 14 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Java, there are four different ways to read input from the user in the command line environment(console). 

1. Using Buffered Reader Class

Buffered Reader Class is the classical method to take input, Introduced in JDK 1.0. This method is used 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. 

  • The input is buffered for efficient reading.
  • The wrapping code is hard to remember.

Example: The below Java program demonstrates how to use BufferReader to read a line of input from the user and print it to the console.

// Java program to demonstrate BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Geeks {
  
    public static void main(String[] args)
        throws IOException
    {
        // Enter data using BufferReader
        BufferedReader r = new BufferedReader(
            new InputStreamReader(System.in));

        // Reading data using readLine
        String s = r.readLine();

        // Printing the read line
        System.out.println(s);
    }
}

Input: 

Geek

Output:

Output

Note: To read other types, we use functions like Integer.parseInt(), Double.parseDouble(). To read multiple values, we use split().

2. Using Scanner Class

Scanner Class is probably the most preferred method to take input, Introduced in JDK 1.5. The main purpose of the Scanner class 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. 

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

Example: The below Java program demonstrates how to use Scanner class to read and display different type of user input.

// Java program to demonstrate working of Scanner Class
import java.util.Scanner;

class Geeks {
    public static void main(String args[])
    {
        // Using Scanner for Getting Input from User
        Scanner s = new Scanner(System.in);

        String s1 = s.nextLine();
        System.out.println("You entered string " + s1);

        int a = s.nextInt();
        System.out.println("You entered integer " + a);

        float b = s.nextFloat();
        System.out.println("You entered float " + b);
    }
}

Input: 

GeeksforGeeks
12
3.4

Output:

Output

3. Using Console Class

Console Class has been becoming a preferred way for reading user’s input from the command line, Introduced in JDK 1.6. 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()). 

Example: The below Java program demonstrates how to use System.console() to read user input from the console, which works in terminal but not in most IDEs.

// Java program to demonstrate working of System.console()
// Note that this program does not work on IDEs as
// System.console() may require console
import java.util.Scanner;

public class Geeks {
    public static void main(String[] args)
    {
        // Using Scanner for Getting Input from User
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a string:");
        String s = scanner.nextLine();

        System.out.println("You entered string " + s);
    }
}

Input: 

GeeksforGeeks

Output:

Output

Advantages:

  • Reading password without echoing the entered characters.
  • Reading methods are synchronized.
  • Format string syntax can be used.
  • Does not work in non-interactive environment (such as in an IDE).

4. Using Command line argument

Command line argument has been around since JDK 1.0 and are used in competitive coding. These inputs are passed to the program during execution and are stored as strings in the args[] array. If we need numeric values, we can convert the strings using methods like Integer.parseInt() or Float.parseFloat(). These programs are run from the command line, with inputs provided while executing the program.

Example: The below Java program demonstrates how to check and print command-line arguments passed to the program or display a message if no arguments are provided.

// Java Program to check for command line arguments
class Geeks {
  
    public static void main(String[] args)
    {
        // check if length of args array is
        // greater than 0
        if (args.length > 0) {
            System.out.println(
                "The command line arguments are:");

            // iterating the args array and printing
            // the command line arguments
            for (String val : args)
                System.out.println(val);
        }
        else
            System.out.println("No command line "
                               + "arguments found.");
    }
}

Command Line Arguments: 

javac Geeks.java
java Main Hello World

Output:

Output

5. Using DataInputStream Class

DataInputStream class in Java, introduced in JDK 1.0, is used to read primitive data types like int, float, boolean, and strings from an input stream in a way that works across different machines. It is part of the java.io package and wraps an existing input stream. It’s commonly used with DataOutputStream or similar sources to ensure the data is read correctly.

Example: The below Java program demonstrates how to use the DataInputStream class to read an integer and a string from user input.

// Java program to read input using DataInputStream class
import java.io.*;

public class Geeks {
  
    public static void main(String[] args)
        throws IOException
    {
        DataInputStream r = new DataInputStream(System.in);

        // Reading integers
        System.out.print("Enter an integer: ");
        int i = Integer.parseInt(r.readLine());

        // Reading strings
        System.out.print("Enter a string: ");
        String s = r.readLine();
        System.out.println("You entered integer: " + i);
        System.out.println("You entered string: " + s);
    }
}

Input:

Enter an integer: 10
Enter a string: GeeksForGeeks

Output:

Output

Please refer this for more faster ways of reading input.



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg