Java User Input
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java User Input

Harine

Java User Input with Scanner

In Java, the most common way to read user input from the keyboard is by using the Scanner
class. It belongs to the java.util package and provides convenient methods to read different data
types such as strings, integers, floating-point numbers, and booleans.

What Is the Scanner Class?

Scanner is a built-in Java class that reads input from various sources, including:

  • Keyboard input (System.in)
  • Files
  • Strings
  • Input streams

For console programs, System.in is typically used.

Basic Example: Reading a String from User Input

import java.util.Scanner;
public class UserInputDemo {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter your full name: ");
    String fullName = scanner.nextLine();
    System.out.println("Welcome, " + fullName + "!");
    scanner.close();
   }
}

Sample Output:

Enter your full name: Kavitha Ravi
Welcome, Kavitha Ravi!

Explanation:

  • new Scanner(System.in) connects the scanner to keyboard input.
  • nextLine() reads an entire line of text.
  • scanner.close() releases system resources (best practice).

Common Scanner Input Methods

The Scanner class provides specialized methods to read different primitive types

Example: Reading Multiple Data Types

import java.util.Scanner;
public class RegistrationForm {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter username: ");
    String username = input.nextLine();
    System.out.print("Enter age: ");
    int age = input.nextInt();
    System.out.print("Enter account balance: ");
    double balance = input.nextDouble();
    System.out.println("\n--- User Details ---");
    System.out.println("Username: " + username);
    System.out.println("Age: " + age);
    System.out.println("Balance: ₹" + balance);
    input.close();
  }
}

Sample Output:

Enter username: arun_k
Enter age: 28
Enter account balance: 15250.75

--- User Details ---
Username: arun_k
Age: 28
Balance: ₹15250.75

Important Note: Input Mismatch Errors

If the user enters a value that does not match the expected type (for example, text instead of a
number), Java throws an InputMismatchException.

Example Problem

Enter age: twenty
Exception in thread "main" java.util.InputMismatchException

Basic Safe Input Handling

import java.util.InputMismatchException;
import java.util.Scanner;

public class SafeInputExample {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    try {
     System.out.print("Enter quantity: ");
     int qty = sc.nextInt();
     System.out.println("Quantity entered: " + qty);
   } catch (InputMismatchException e) {
       System.out.println("Invalid input. Please enter a whole number.");
   } finally {
       sc.close();
   }
  }
}

Best Practices When Using Scanner

  • Always close the scanner (scanner.close())
  • Validate numeric input when necessary
  • Use nextLine() after numeric input to clear newline if mixing types
  • Prefer one Scanner per input stream

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !