How to take String Input in Java?

When learning Java, handling user input is one of the first things you need to master. Strings, which are sequences of characters, are one of the most commonly used data types in Java. In this guide, you’ll learn the different ways to take string input in Java, along with the best practices and examples.

1. Using Scanner Class (Most Common Method)

The Scanner class in Java is the most commonly used method to take user input. It is part of java.util package and provides an easy way to read strings, integers, and other data types. Also learn How to take Integer Input in Java.

Example:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter your name: ");
        String name = scanner.nextLine();  // Reads the full line as input
        
        System.out.println("Hello, " + name + "!");
        
        scanner.close();  // Close scanner to prevent memory leaks
    }
}
Enter your name: Dawood
You entered: Dawood

Explanation:

  • Scanner scanner = new Scanner(System.in); → Creates a Scanner object for user input.
  • scanner.nextLine(); → Reads a full line of text (including spaces).
  • scanner.close(); → Closes the Scanner to free up resources.

Alternative: Using next() Method

If you only need a single word (without spaces), use scanner.next() instead:

String word = scanner.next();

However, next() stops reading at whitespace, so it’s not ideal for full sentences.

2. Using BufferedReader (Faster for Large Input)

If you’re handling large amounts of text input, BufferedReader is more efficient.

Example:

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        
        System.out.print("Enter your favorite quote: ");
        String quote = reader.readLine();
        
        System.out.println("Your quote: " + quote);
    }
}

Explanation:

  • BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); → Creates a BufferedReader object.
  • reader.readLine(); → Reads an entire line of input efficiently.
  • This method is faster than Scanner but requires handling IOException.

3. Using Console Class (Recommended for Secure Input)

For applications that require password input or secure data entry, Console is a better choice.

Example:

import java.io.Console;

public class Main {
public static void main(String[] args) {
Console console = System.console();

    if (console != null) {
        String username = console.readLine("Enter username: ");
        char[] password = console.readPassword("Enter password: ");

        System.out.println("Welcome, " + username + "!");
    } else {
        System.out.println("Console not available");
    }
   }
  }

Explanation:

  • console.readLine(); → Reads a normal string input.
  • console.readPassword(); → Hides password input for security.
  • Works only in terminal/command line, not in IDEs like Eclipse or IntelliJ.

4. Using Command-Line Arguments (Alternative Approach)

Another way to get input is through command-line arguments.

Example:

public class Main {
    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.println("Input received: " + args[0]);
        } else {
            System.out.println("No input provided!");
        }
    }
}

Explanation:

  • The args array stores values passed via the command line.
  • To run: java Main Hello → Output: Input received: Hello.

Best Learning Approach: When to Use Each Method?

MethodUse Case
ScannerBest for general user input in small programs
BufferedReaderBest for large text input, more performance-efficient
ConsoleBest for secure input like passwords
Command-Line ArgsBest for passing predefined values

In Summary

Taking string input in Java is essential for interactive programs. The Scanner class is the easiest method, but BufferedReader is better for performance, and Console is ideal for secure input. Choosing the right method depends on your needs.

Leave a Comment