# Scanner

**作用**：获取用户的键盘输入，他是基于正则表达式的文本扫描器。可以从文件、输入流、字符串中解析出基本类型和字符串的值。

主要提供两个方法来扫描输入：

* `hasNextXxx()`:是否还有下一个输入项
* `nextXxx()`:获取下一个输入项。

默认情况下，Scanner以 空格、Tab、回车作为多个输入项之间的分隔符，我们也可以使用`useDelimiter("分割符")`来设置分隔符。

**阻塞**： Scanner读取操作（hasNext与next）有可能会被阻塞（当前执行顺序流暂停），从而等待信息输入。

**逐行读取，不受文输入型限制**：

```java
boolean  hasNextLine(); 是否还有下一行
String nextLine(); 返回下一行字符串
```

## 小问题

```java
package me.feathers.scannerdemo;
import java.util.Scanner;

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

        System.out.print("请您输入体重：");
        int weight = sc.nextInt();
        System.out.println("你的体重为"+weight);
        //此处的nextLine()失效了,程序将不再进入到阻塞状态了.（因为前面有nextInt/nextDouble，如果没有则不会出现该问题）【小问题】
        System.out.print("请您输入信息：");
        String meassage = sc.nextLine();
        System.out.println("你输入的内容为"+meassage);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yangsx95.gitbook.io/notes/programming-language/java/chang-yong-api/scanner.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
