Home > Java > javaTutorial > body text

How to use the scanner class in java

下次还敢
Release: 2024-05-01 19:36:59
Original
307 people have browsed it

The Scanner class is used to read input from users or files. It provides methods for reading different data types: Import Scanner package: import java.util.Scanner Create Scanner object: Scanner(InputStream) from the input stream Read, Scanner(File) reads input from the file: next() reads words, nextInt() reads integers, nextDouble() reads floating point numbers, nextLine() reads a line of text Close Scanner: close() Method to release resources and prevent resource leaks

How to use the scanner class in java

How to use the Scanner class in Java

Introduction

The Scanner class is used to Input data is read from user input or file, and it provides a convenient interface to handle different data types.

Usage method

  1. Import class: Import the java.util.Scanner package in the code.
  2. Create a Scanner object: Create a Scanner object using one of the following constructors:

    • Scanner(InputStream source): From the given input stream Read data.
    • Scanner(File file): Read data from the specified file.
  3. Read input: Use the following methods to read different types of data:

    • next(): Read Next word (separated by spaces).
    • nextInt(): Read the next integer.
    • nextDouble(): Read the next floating point number.
    • nextLine(): Read a line of text, including spaces.
  4. Close Scanner: Use the close() method to close the Scanner object and release resources.

Sample code

<code class="java">import java.util.Scanner;

public class Main {
    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.print("请输入您的身高(单位:厘米):");
        double height = scanner.nextDouble();

        // 输出结果
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
        System.out.println("身高:" + height);

        // 关闭 Scanner
        scanner.close();
    }
}</code>
Copy after login

Notes

  • When using the nextLine() method to read text, you need to call nextLine() an additional time to ignore the newline at the end of the line symbol.
  • When using Scanner, you need to handle input exceptions, such as number format exceptions (NumberFormatException).
  • Be sure to call the close() method after using the Scanner to release resources and avoid resource leaks.

The above is the detailed content of How to use the scanner class in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template