Home > Java > javaTutorial > body text

How to import scanner in java

little bottle
Release: 2020-09-18 09:12:12
Original
15328 people have browsed it

java.util.Scanner is a new feature of Java5, which is a new utility for scanning input text. Scanner is some combination between the previous StringTokenizer and Matcher classes. Since any data must be retrieved through capturing groups of the same schema or by using an index to retrieve individual parts of the text.

How to import scanner in java

You can then use regular expressions in conjunction with methods for retrieving specific types of data items from the input stream. In this way, in addition to using regular expressions, the Scanner class can also analyze data of strings and basic types (such as int and double) arbitrarily. With Scanner, you can write a custom parser for any text content you want to process.

import java.util.Scanner;
Copy after login

Add the above command to the location below the import package.

The following is the basic syntax for creating a Scanner object:

import java.util.Scanner; //导入Scanner包
 
public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);        // 从键盘接收数据
 
        // next方式接收字符串
        System.out.println("next方式接收:");        // 判断是否还有输入
        if (scan.hasNext()) {
            String str1 = scan.next();            
        System.out.println("输入的数据为:" + str1);        }
        scan.close();    }}
Copy after login

Related learning recommendations: java basic tutorial

The above is the detailed content of How to import scanner 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!