Scanner Class for User Input in Java

Introduction

In Java, the Scanner class is used to take input from the user through the keyboard. It is part of the java.util package and provides various methods to read different types of data, such as integers, floating-point numbers, strings, and more. Using the Scanner class makes programs interactive, allowing users to provide input during runtime.


Code: Using Scanner for User Input

import java.util.Scanner; // Importing Scanner class
public class UserInputExample {
public static void main(String[] args) {
// Create a Scanner object
Scanner scanner = new Scanner(System.in);
// Reading an integer from user
System.out.print("Enter your age: ");
int age = scanner.nextInt();
// Reading a floating-point number from user
System.out.print("Enter your height in meters: ");
float height = scanner.nextFloat();
// Reading a string from user
System.out.print("Enter your name: ");
scanner.nextLine(); // Consume leftover newline
String name = scanner.nextLine();
// Displaying the user input
System.out.println("\nUser Details:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height);
// Closing the Scanner object
scanner.close();
}
}

Explanation of Each Code Part

1. Importing Scanner

import java.util.Scanner;
  • Scanner is part of the java.util package, so we need to import it.
  • This allows us to use Scanner to read input from the user.

2. Creating a Scanner Object

Scanner scanner = new Scanner(System.in);
  • Creates a new Scanner object named scanner.
  • System.in indicates that input will come from the keyboard.

3. Reading an Integer

int age = scanner.nextInt();
  • nextInt() reads an integer value from the user.
  • Stores the value in the variable age.

4. Reading a Float

float height = scanner.nextFloat();
  • nextFloat() reads a floating-point number from the user.
  • Stores the value in the variable height.

5. Reading a String

scanner.nextLine(); // Consume leftover newline
String name = scanner.nextLine();
  • nextLine() reads a line of text (string) from the user.
  • We use scanner.nextLine() once before to consume the leftover newline character from nextInt() or nextFloat().

6. Displaying User Input

System.out.println("Name: " + name);
  • Prints the user input to the console.
  • + is used to concatenate strings with variables.

7. Closing the Scanner

scanner.close();
  • Closes the Scanner object.
  • Important to free system resources and avoid memory leaks.

Conclusion

The Scanner class is a simple and effective way to take user input in Java.

  • It can read integers, floating-point numbers, strings, and more.
  • Always remember to close the Scanner object after use.
  • Proper handling of newline characters ensures correct reading of string input after numeric input.

This program demonstrates how to make your Java programs interactive by allowing users to provide input during runtime.

Leave a Reply

Your email address will not be published. Required fields are marked *


Macro Nepal Helper