Java From Beginner To Advanced

0% completed

Previous
Next
Java Basics of Input

In Java, user input is commonly handled using the Scanner class. This class, part of the java.util package, provides methods to read various types of data such as integers, floating-point numbers, strings, and booleans from the user.

What is the Scanner Class?

The Scanner class allows programs to read input from a variety of sources, including the keyboard. It is part of Java's standard library, so you only need to import it at the beginning of your program:

import java.util.Scanner;

Setting Up the Scanner

To use the Scanner, you first create an object of the Scanner class. Below is the syntax:

Scanner scanner = new Scanner(System.in);
  • Scanner: The class name to create a Scanner object.
  • scanner: The name of the Scanner object (can be any valid variable name).
  • System.in: Refers to the standard input stream, which is the keyboard.

Reading Different Types of Input

The Scanner class provides specific methods to read input depending on the data type. Here are some common methods:

MethodDescriptionExample Input
nextInt()Reads an integer10
nextDouble()Reads a floating-point number3.14
next()Reads a single word (until a space)Hello
nextLine()Reads an entire line of textHello World
nextBoolean()Reads a boolean value (true/false)true

Example: Reading an Integer

This example demonstrates how to use the Scanner class to read an integer input from the user.

Java
Java
. . . .

Explanation:

  1. Scanner scanner = new Scanner(System.in);: Creates a Scanner object named scanner to read input from the keyboard (System.in).
  2. System.out.print("Enter an integer: ");: Displays a message prompting the user to enter an integer. The print() method is used to keep the cursor on the same line.
  3. int number = scanner.nextInt();: Reads the integer input from the user and stores it in the variable number.
  4. System.out.println("You entered: " + number);: Prints the value of number to confirm the input.
  5. scanner.close();: Closes the Scanner object to release system resources.

Example: Reading a String and a Double

In this example, we use the Scanner class to read a string (name) and a double (favorite number).

Java
Java
. . . .

Explanation:

  1. Scanner scanner = new Scanner(System.in);: Creates a Scanner object to read input.
  2. System.out.print("Enter your name: ");: Displays a prompt asking the user for their name.
  3. String name = scanner.nextLine();: Reads a full line of input (including spaces) entered by the user and stores it in the name variable.
  4. System.out.print("Enter your favorite number: ");: Prompts the user to input a number.
  5. double favoriteNumber = scanner.nextDouble();: Reads a double (floating-point number) input and stores it in favoriteNumber.
  6. System.out.println(...): Combines the name and favoriteNumber variables into a message and prints it.
  7. scanner.close();: Closes the Scanner to release resources.

The Scanner class makes it easy to create interactive Java programs by reading user input. Experiment with the examples provided and try reading different types of input to become familiar with its methods.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next