当前位置:网站首页>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 .
边栏推荐
猜你喜欢

centos更改mysql数据库目录

『每日一问』LockSupport怎么实现线程等待、唤醒

Wechat applet obtains the data of ---- onenet and controls the on-board LED of STM32

Idea hot deployment

API健康状态自检
![[arm] Xintang nuc977 transplants wk2124 drive](/img/0c/fa21d7a44e0263dde4d3135a78818f.png)
[arm] Xintang nuc977 transplants wk2124 drive

卷积神经网络的兴趣简单介绍
![[WSN communication] optimize HWSN energy-saving clustering protocol based on MATLAB biogeography [including Matlab source code, 1989]](/img/47/c804acc3f8de1f0cd5938c3d889e46.png)
[WSN communication] optimize HWSN energy-saving clustering protocol based on MATLAB biogeography [including Matlab source code, 1989]
![[GYCTF2020]Ez_Express](/img/ce/02b90708f215715bb53cacfd4c21f0.png)
[GYCTF2020]Ez_Express

在Ubuntu中安装MySQL并创建新用户
随机推荐
Comparison between symmetric encryption and asymmetric encryption
Redis的十大常见应用场景
office文件对应的Content-Type类型
SSM高级整合
@Scheduled源码解析
Idea practical tips --- now change pom.xml (red) to pom.xml (blue)
[De1CTF 2019]SSRF Me
[common tools] obtain system status information based on psutil and gputil
实现简单的RESTful API服务器
ActiveMQ -- leveldb of persistence mechanism
Week小结
机器学习 —— Sklearn包中StandardScaler()、transform()、fit()的详细介绍
Unable to start debugging on the web server, the web server failed to find the requested resource
分享一个避免递归的部门设计方法
How can technologists start their personal brand? Exclusive teaching of top five KOLs
微信小程序中的列点击隐藏,再点击显示
『怎么用』观察者模式
Activemq-- delayed delivery and scheduled delivery
Silicon Valley classroom lesson 12 - official account on demand course and live broadcast management module
Floating point number exploration