当前位置:网站首页>Detailed explanation of the use of nanny scanner class
Detailed explanation of the use of nanny scanner class
2022-07-25 09:26:00 【Work hard, work hard, gzc】
One 、Scanner Class introduction
Scanner Class is in jdk1.5 Version introduced , It's in java Of util Under tool kit , It is mainly used to scan the text entered by the user from the console .
When we need to input data through the console , Just import in advance java.util In bag Scanner class , And then call Scanner class ,
Our program can get the data we input on the console .
Two 、 How to use Scanner class
1、 First, import. java.util.Scanner package ;
import java.util.Scanner;
2、 establish Scanner Class object
Scanner sc = new Scanner(System.in);
3、 Create a variable to receive the input data
// Use the above created sc Object calls the corresponding method , The console can wait for user input , We can customize a variable to receive
// We want to input different types of data , You also need to call different methods , The specific application is as follows :
String str = sc.nextLine();
String str = sc.next();
byte a1= sc.nextByte();
short a2= sc.nextShort();
int a3 = sc.nextInt();
long a4 = sc.nextLong();
float a5 = sc.nextFloat();
double a6 = sc.nextDouble();
boolean a7 = sc.nextBoolean();
4、 close Scanner class
// Use up Scanner after , We must remember to turn it off !
// Because use Scanner In essence, it opens a IO flow , If it's not closed , It will always occupy system resources .
sc.close();
// But be careful once you turn it off , Even in sc.close() After this line of code, you can re new Scanner(System.in), Then you can't turn on a scanner again , As shown in the figure below, you will find that the program will report errors
// So you must turn off the scanner after you don't need it , Namely the sc.close() Put the code at the end .

3、 ... and 、next() and nextLine() The difference between ( Very important )
From the above explanation , We will find that when the data type we want to input is String when , We have two methods to call :next() and nextLine(). What is the difference between these two methods ? Let's move on :
(1) Let's test it first next():
After using multiple sets of test cases , It is not difficult to draw the following conclusions by analyzing the output results ( Only classic test cases are posted here , Others can test by themselves ):
next() Method when identifying the input string , We can't get the string with spaces we entered , Just read hello.
This explanation next() After reading a valid character, it will take a space as its own reading end identifier , Because we can find hello The preceding space does not affect the reading of the method . therefore next() The usage is summarized as follows :
next() Usage Summary :
1. You must read valid characters before you can end the input .
2. The blank space encountered before the valid characters entered , It will be automatically removed .
3. Only after a valid character is entered, the blank space entered after it is used as the closing character .
4. next() Cannot get string with space .
5. After reading , This method will position our mouse on the line where we input data .
(2) Let's test it again nextLine():
The analysis found that , The input result is completely consistent with the input result ,nextLine() The reading result is not affected by the space in the middle of the string .
This is because the method uses carriage return ( That is our keyboard enter key ) As an end identifier .
nextLine() Usage Summary :
1、 Take carriage return as the end identifier , Get all the strings entered before the carriage return ( Including Spaces ).
2、 After reading , This method will position our mouse on the next row of the row we input data .
(3) First use nextLine Reuse next()、nextInt() No problem waiting , But first use next() and nextInt() After that, you can't follow closely nextLine Use .( This is important !!!)
This is because next() After these methods are read, a carriage return will follow , and nextLine The carriage return will be read directly , This leads to the fact that we haven't had time to input the data we want to input ,nextLine I thought we had entered such a situation !
The solution is simple : We are directly in the next() Add two after use nextLine() Just OK 了 , So the first one nextLine() Will be one ‘ For the dead ’, the second nextLine() We can input the data we want to input !
Four 、hash×××() Simple use
We found that Scanner The instance variable of the class will also have hash The first set of methods , What are these methods for ?
Let's go directly to the test case , You can intuitively experience :
5、 ... and 、Scanner Method summary 
The meaning of this figure is actually , such as :nextlnt(): Can only read int value , If the user enters other non integer data , The program will report an error .
Others such as nextByte()、nextDouble() And so on , Only data that matches this type can be read , So we need to match the corresponding method according to the data we enter .
边栏推荐
- [GKCTF 2021]easynode
- The interviewer asked: how to prevent oversold? There are several ways to achieve it
- MySQL takes the query result as the data updated by update, and concatenates it after the original field data (Lej)
- Reverse Integer
- 什么是单机、集群与分布式?
- 卷积神经网络的兴趣简单介绍
- 保姆级Scanner类使用详解
- 『每日一问』怎么实现一个正确的双重检查锁定
- Leetcode-238. product of arrays other than itself
- 在Ubuntu中安装MySQL并创建新用户
猜你喜欢

『怎么用』装饰者模式

什么是单机、集群与分布式?

Unable to start debugging on the web server, the web server failed to find the requested resource

Network principle (2) -- network development

How can technologists start their personal brand? Exclusive teaching of top five KOLs

activemq--可持久化机制之JDBC代码

Nacos启动报错Unable to start web server

Silicon Valley classroom lesson 15 - Tencent cloud deployment

使用nexus3发布yum私服(离线-内网)

TCP网络应用程序开发流程
随机推荐
Silicon Valley classroom lesson 15 - Tencent cloud deployment
redis操作利用游标代替keys
28. Slot
神经网络方法——美国波士顿房价(回归问题)
c语言中的六个存储类型:auto register static extern const volatile
机器学习 —— Sklearn包中StandardScaler()、transform()、fit()的详细介绍
卷积神经网络的兴趣简单介绍
How can technologists start their personal brand? Exclusive teaching of top five KOLs
[De1CTF 2019]SSRF Me
MongoDB数据库文件的读与写
yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。
数据预处理
Redis-哨兵,主从部署详细篇
idea 热部署
activemq--可持久化机制之KahaDB
API健康状态自检
Activemq-- delayed delivery and scheduled delivery
Flask SSTI注入学习
Analysis of concat and group in MySQL_ Use of concat
Silicon Valley class lesson 11 - official account news and wechat authorization