Home >Java >javaTutorial >How to use Scanner function in Java for user input operations

How to use Scanner function in Java for user input operations

WBOY
WBOYOriginal
2023-06-26 16:00:413993browse

Scanner in Java is a very useful function for receiving user input and processing it in the program. This article will introduce how to use the Scanner function for user input operations.

  1. Import the Scanner class

Before using the Scanner class, you must first introduce it. In Java, input operations usually need to be completed through System.in, so you can use the following code:

import java.util.Scanner;

This command will introduce the java.util package, which contains the Scanner class.

  1. Creating a Scanner object

Once import is used, a Scanner object can be created. The method to create a Scanner object is as follows:

Scanner scanner = new Scanner(System.in);

This code will create a Scanner object and associate it with the standard input stream (System.in).

  1. Use Scanner object to read input

Getting input from Scanner object is relatively simple. The Scanner class provides multiple methods for reading different types of input. The following are some common methods of Scanner:

  • nextLine(): Read the entire line of input
  • next(): Read the next word
  • nextInt() : Read the next integer
  • nextDouble(): Read the next double value

The following is a sample program that demonstrates how to use Scanner to read user input:

import java.util.Scanner;

public class UserInputDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入您的名字:");
        String name = scanner.nextLine();
        System.out.print("请输入您的年龄:");
        int age = scanner.nextInt();
        System.out.println("您好," + name + ",您今年" + age + "岁了。");
    }
}

In the above example program, a Scanner object scanner is first created, and then the name and age input by the user are read using the scanner.nextLine() and scanner.nextInt() methods respectively. Finally output these inputs to the console.

It should be noted that the nextLine() method of the Scanner class will include the carriage return character at the end of the line when reading the input. If you want to filter out carriage returns, you can use the trim() method after reading, as shown below:

String input = scanner.nextLine().trim();

In addition, if the type input by the user is inconsistent with the type required by the program, the program will throw An InputMismatchException exception occurs. In this case, you can use a try-catch block to catch the exception and prompt the user to re-enter.

Summary:

In Java, it is very simple to use the Scanner class for input operations. Just introduce the Scanner class, create a Scanner object, and use the methods it provides to read user input. When reading input, consider the type and format of the input and process it appropriately.

The above is the detailed content of How to use Scanner function in Java for user input operations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn