S07L12 – User input (Basics)

User Input in Java Using the Scanner Class

Table of Contents

  • Introduction
  • Understanding User Input in Java
  • The Scanner Class in Java
  • Example Code and Explanation
  • Handling Errors with Input Mismatch Exception
  • Conclusion

1. Introduction

User input is a fundamental part of any interactive Java program, where the program needs to receive data from users.
This article covers how to accept user input using the Scanner class in Java. We’ll explore its significance,
common uses, and how to implement it step by step.

Why is it important?

Handling user input is vital for dynamic applications where the outcome of the program depends on user-provided values.
In Java, the Scanner class from the java.util package provides an easy way to capture user input from the console.

2. Understanding User Input in Java

Java offers multiple ways to capture user input. The Scanner class is one of the most commonly used approaches
for console input due to its simplicity and versatility. Here, we will break down the process of getting user input and
handling it within a Java application.

Basic Requirements:

  • Java Scanner is included in the java.util package.
  • No additional dependencies are required for using the Scanner class.

The syntax to create a scanner object:

3. The Scanner Class in Java

The Scanner class is used to read data of various types (like integers, strings, floats, etc.) from different
input sources, such as the keyboard or files. Here, we are interested in reading input from the console.

Key Methods of the Scanner Class:

Method Description
nextInt() Reads an integer value from the input
nextFloat() Reads a float value
nextLine() Reads a string until the end of the line
nextDouble() Reads a double value

4. Example Code and Explanation

Here’s a simple example program that prompts the user for their age and then outputs the entered value:

Explanation:

  • Importing the Scanner class: The Scanner class is part of the java.util package, so it needs to be imported.
  • Creating a Scanner object: A scanner object is instantiated to capture input from the console.
  • Prompting the user for input: The program asks the user to input their age.
  • Capturing the input: The nextInt() method is used to capture an integer value from the user input.
  • Displaying the result: The program prints the captured age to the console.
  • Closing the Scanner: The scanner.close() method is called to free up the resources.

Output Example:

5. Handling Errors with Input Mismatch Exception

One of the most common mistakes when using the Scanner class is providing input that doesn’t match the expected data type.
For example, if the program expects an integer but receives a string or decimal, it throws an InputMismatchException.

Example of Incorrect Input:

Solution:

You can handle this exception by using a try-catch block:

Explanation of the Improved Code:

  • The try block attempts to capture the user input and convert it to an integer.
  • The catch block handles the InputMismatchException if the input is invalid.
  • The finally block ensures that the scanner is closed after the program runs, regardless of whether an exception occurs.

6. Conclusion

In this article, we learned how to capture user input using the Scanner class in Java. We explored the basic syntax,
common methods, and how to handle exceptions when the input does not match the expected type. Incorporating user input makes
programs dynamic and interactive, allowing them to respond to various conditions based on user-provided data.