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.
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;
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(); }}
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!