Handling User Input with Scanner

Tutorial 4 of 5

1. Introduction

In this tutorial, we will learn how to handle user input using the Scanner class in Java. This class is a convenient way to get input from the user in the console. By the end of this tutorial, you will be able to read different types of input from the user, including strings, integers, and floating-point numbers.

Prerequisites:

  • Basic knowledge of Java programming language
  • JDK installed on your computer
  • A text editor or IDE (like Eclipse or IntelliJ IDEA)

2. Step-by-Step Guide

The Scanner class is part of the java.util package, so we need to import it before using. The Scanner class can read from various input streams including reading from a string or file. In this tutorial, we'll focus on reading from standard input (System.in).

Creating a Scanner object:

To start, we need to create an instance of the Scanner class. Here's the syntax:

Scanner scanner = new Scanner(System.in);

Reading Input:

The Scanner class provides various methods to read different types of data:

  • nextLine(): reads a line of text
  • nextInt(): reads an integer
  • nextDouble(): reads a double
  • next(): reads the next token

Each method advances the scanner past the input that matched.

3. Code Examples

Example 1: Reading a line of text

import java.util.Scanner;  // Import the Scanner class

public class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter your name:");

    String userName = myObj.nextLine();  // Read user input
    System.out.println("Your name is: " + userName);  // Output user input
  }
}

In this example, myObj.nextLine(); waits for the user to input a line of text in the console. The text is then stored in the userName variable, and printed out.

Example 2: Reading an integer

import java.util.Scanner;  // Import the Scanner class

public class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter your age:");

    int userAge = myObj.nextInt();  // Read user input
    System.out.println("Your age is: " + userAge);  // Output user input
  }
}

Here, myObj.nextInt(); waits for the user to input an integer. The integer is then stored in the userAge variable, and printed out.

4. Summary

In this tutorial, we learned how to handle user input using the Scanner class in Java. We have seen how to create a Scanner object, and how to use different methods provided by the Scanner class to read different types of user input.

For further learning, you could explore how to read other types of data like boolean or long, and how to use the Scanner class to read from a file.

5. Practice Exercises

Exercise 1: Write a program that asks the user for their name and age, and prints them.

Solution:

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter your name:");
    String name = scanner.nextLine();

    System.out.println("Enter your age:");
    int age = scanner.nextInt();

    System.out.println("Your name is " + name + " and you are " + age + " years old.");
  }
}

Exercise 2: Write a program that asks the user for two numbers, and prints the sum.

Solution:

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter the first number:");
    int num1 = scanner.nextInt();

    System.out.println("Enter the second number:");
    int num2 = scanner.nextInt();

    int sum = num1 + num2;
    System.out.println("The sum is " + sum);
  }
}

For further practice, you can modify these exercises to handle different types of input, or to perform different operations on the input.