How to Take Input in Java: Scanner, BufferedReader and Console

To take input in Java, use Scanner for general use, BufferedReader for high-speed input, or Console for secure password reading. Scanner is the simplest: import java.util.Scanner, create new Scanner(System.in), and call nextLine() or nextInt() to read user input from the keyboard.

  • Scanner is the go-to choice for beginners; it parses tokens directly and works well for reading integers, doubles and strings.
  • BufferedReader reads entire lines as strings and is significantly faster for competitive programming or file-heavy applications.
  • Console is useful for reading passwords securely but does not work inside most IDEs like IntelliJ or Eclipse.
  • The most common bug beginners hit is nextLine() skipping input after nextInt(). There is a simple fix for it.
  • Strong input-handling skills matter beyond Java too; they carry directly into backend development, ethical hacking and secure coding roles.

Ways to Read Input in Java

Java gives you three main classes for reading user input from the command line. Each one sits in a different package and is built for a slightly different scenario. Picking the right one saves you debugging time later.

Class Package Best For Speed Works in IDE?
Scanner java.util General use, assignments, interviews Moderate (~1x baseline) Yes
BufferedReader java.io Competitive programming, large input Fast (~10-15x faster than Scanner for large input) Yes
Console java.io Password input, terminal-only apps Moderate No (returns null)

According to the Stack Overflow Developer Survey 2024, Java remains one of the top five most-used programming languages globally, with strong adoption in enterprise and academic settings across India. According to the JetBrains State of Developer Ecosystem 2023, Java is used by 57% of developers in backend web development, making input-handling fundamentals directly relevant to real-world work. The TIOBE Index (May 2025) consistently ranks Java in the top four programming languages worldwide, reflecting its continued dominance in Indian IT placements and enterprise software. That means the input techniques you learn here will show up in real codebases, not just lab practicals.

If you are just getting started with programming tools and environments, it is worth reading about software development tools to learn first before you go deep on any single language.

How to Take Input in Java Using Scanner

Scanner is defined in java.util.Scanner and wraps System.in to give you easy methods for reading different data types. You import it, create an object, and call the right method. That is it for most cases.

How to Read a String with Scanner

Use next() to read a single word, or nextLine() to read a full line including spaces. For a student entering their full name at an Indian university portal, you would use nextLine().

import java.util.Scanner;

public class ReadString {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your full name: ");
        String name = sc.nextLine();
        System.out.println("Hello, " + name);
        sc.close();
    }
}

Here is the pattern in plain terms:

  1. Import java.util.Scanner at the top of your file.
  2. Create a Scanner object: Scanner sc = new Scanner(System.in);
  3. Call sc.nextLine() to capture the full input string.
  4. Store it in a String variable and use it.
  5. Close the scanner with sc.close(); when you are done.

How to Read Integer and Double Input in Java with Scanner

Scanner has dedicated methods for every primitive type. nextInt() reads an integer, nextDouble() reads a decimal, and nextLong() handles large numbers. These methods parse the token directly, so you do not need to convert strings manually.

Method Data Type Example Input Returns
nextInt() int 42 42 as int
nextDouble() double 3.14 3.14 as double
nextLong() long 9876543210 9876543210 as long
nextLine() String Hello World “Hello World”
nextBoolean() boolean true true as boolean
import java.util.Scanner;

public class ReadNumbers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your age: ");
        int age = sc.nextInt();
        System.out.print("Enter your GPA: ");
        double gpa = sc.nextDouble();
        System.out.println("Age: " + age + ", GPA: " + gpa);
        sc.close();
    }
}

One practical tip: if your program takes multiple inputs in sequence, declare a single Scanner instance at the top and reuse it. Creating a new Scanner(System.in) inside a loop causes resource leaks and sometimes throws exceptions. Speaking of exceptions, it is worth understanding Java exceptions and common runtime errors so you know what to do when Scanner throws an InputMismatchException.

How to Take Input in Java Using BufferedReader

BufferedReader from java.io wraps an InputStreamReader around System.in. It reads entire lines as strings, which means you parse the data yourself. That extra step is worth it when speed matters.

A benchmark published on GeeksforGeeks shows BufferedReader can be roughly 10 to 15 times faster than Scanner for large inputs, which is why competitive programmers on platforms like Codeforces, CodeChef and LeetCode default to it. MCA and BTech students preparing for placements at product companies should get comfortable with both styles.

Setting Up BufferedReader

  1. Import java.io.BufferedReader and java.io.InputStreamReader.
  2. Create the reader: BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  3. Read a line: String line = br.readLine();
  4. Parse numbers using Integer.parseInt(line) or Double.parseDouble(line).
  5. Wrap the whole block in a try-catch for IOException, or declare it in the method signature with throws IOException.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class ReadWithBufferedReader {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter your name: ");
        String name = br.readLine();
        System.out.print("Enter your roll number: ");
        int roll = Integer.parseInt(br.readLine());
        System.out.println("Name: " + name + ", Roll: " + roll);
    }
}

Reading multiple integers on one line, which is common in competitive problems on HackerEarth and Codeforces, looks like this in concept: read the line, split by space, then parse each token. It is one extra line of code compared to Scanner, but the performance gain at scale is real.

When to Use Console for Java User Input

Console is accessed via System.console(), which returns a Console object or null if there is no terminal attached. Its main advantage is readPassword(), which reads input without echoing characters to the screen. That is useful for CLI tools, but it returns null inside IntelliJ, Eclipse or VS Code, so you cannot test it there. Stick to Scanner or BufferedReader for coursework and interviews.

Common Pitfalls When Taking Input in Java

This is where most beginners lose marks and most interview candidates waste time. The pitfalls below are real, recurring problems seen in BTech and MCA lab sessions at Indian universities and in online coding rounds.

The nextLine() Skip Bug

This is the single most common mistake when learning how to take input in Java. When you call nextInt() or nextDouble(), Scanner reads the number but leaves the newline character (\n) sitting in the buffer. The next nextLine() call immediately consumes that leftover newline and returns an empty string.

The fix is simple: add a dummy sc.nextLine(); call right after nextInt() to consume the leftover newline, then call nextLine() again to read the actual string. Alternatively, read everything as strings with nextLine() and parse numbers manually using Integer.parseInt().

Not Closing the Scanner

Leaving a Scanner open does not crash small programs, but it causes resource leaks in production code. Always call sc.close(); at the end, or use a try-with-resources block: try (Scanner sc = new Scanner(System.in)) { ... }. Java handles the close automatically that way.

InputMismatchException at Runtime

If the user types “abc” when your code expects an integer, Scanner throws an InputMismatchException. This is a runtime exception, not a compile-time error, so your IDE will not warn you. Validate input or wrap Scanner calls in try-catch blocks for any production-grade code. This connects directly to why understanding Java exception handling is non-negotiable for any serious Java developer.

Using Console in an IDE

System.console() returns null when there is no real terminal. Calling any method on a null reference throws a NullPointerException. If you are testing locally in an IDE, always use Scanner or BufferedReader instead. Save Console for deployed command-line tools.

If you are curious how input handling compares across languages, check out how to write and run a Python program and how to write, compile and run a C program for side-by-side context. The concepts transfer; the syntax just changes.

Students pursuing a BSc Computer Science or MCA will encounter Java input handling in their very first programming semester. Getting this right early builds the kind of foundation that matters when you are looking at jobs and salary after MCA in software development and backend engineering roles.

Frequently Asked Questions

How do I take input in Java?

Use the Scanner class from java.util for most cases. Create a Scanner object with new Scanner(System.in), then call methods like nextLine(), nextInt() or nextDouble() to read user input from the keyboard. It is the simplest and most widely taught approach in Indian university curricula and coding interviews.

Scanner vs BufferedReader: which should I use?

Use Scanner when you need to read different data types easily and your input volume is small to medium. Use BufferedReader when performance matters, such as in competitive programming problems with thousands of input lines. BufferedReader reads raw strings and is measurably faster; Scanner parses tokens automatically, which is more convenient for beginners.

Why does nextLine() skip input after nextInt()?

nextInt() reads the integer but leaves the newline character in the input buffer. The next nextLine() call picks up that leftover newline and returns an empty string instead of waiting for new input. Fix it by calling a dummy sc.nextLine(); right after nextInt() to clear the buffer before reading the actual string.

How do I read a string in Java?

Use sc.next() to read a single word, or sc.nextLine() to read a full line including spaces. If you are using BufferedReader, call br.readLine(), which always returns the entire line as a String. For names, addresses or any multi-word input, nextLine() or readLine() is the right choice.

How do I read numbers in Java from user input?

With Scanner, call sc.nextInt() for integers, sc.nextDouble() for decimals or sc.nextLong() for large numbers. With BufferedReader, read the line as a string and parse it: int n = Integer.parseInt(br.readLine());. Both approaches work correctly; the BufferedReader method gives you more control over error handling.

How do I take multiple inputs in one line in Java?

With Scanner, call nextInt() or next() multiple times on the same line; Scanner splits by whitespace automatically. With BufferedReader, read the full line using br.readLine(), then split it with line.split(" ") and parse each token individually. The BufferedReader approach is preferred in competitive programming on platforms like CodeChef and Codeforces.

How do I take array input in Java using Scanner?

Declare an array of the required size, then use a loop to call sc.nextInt() for each element. For example: int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); }. This pattern is standard in BTech lab assignments and placement coding rounds across India.

The short version: Scanner is your everyday tool for how to take input in Java, BufferedReader is your performance tool, and the nextLine-skip bug is the one pitfall that catches almost every Java beginner at least once. Fix that bug early, close your resources properly, and you will write cleaner Java than most of your peers from day one.

  1. Write a small Java program this week that reads a user’s name and age using Scanner, intentionally reproduce the nextLine-skip bug, then apply the fix so you understand it in your own code.
  2. Rewrite the same program using BufferedReader and Integer.parseInt() to see the structural difference firsthand.
  3. Explore how these fundamentals apply in real-world security and development work. Strong programming basics are the foundation for ethical hacking, secure coding, AI and blockchain development. Check out 3.0 University’s CEH v13 AI online course or browse the full 3.0 University certification catalogue to see where your Java skills can take you next.

Last updated: June 2025. Reviewed by the 3University editorial team.

We create, review, and maintain educational content covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain, Web3, and emerging technologies.

Read More Articles