当前位置:网站首页>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 .
边栏推荐
- Comparison between symmetric encryption and asymmetric encryption
- Ten thousand words long, one word thoroughly! Finally, someone has made business intelligence (BI) clear
- [arm] Xintang nuc977 transplants wk2124 drive
- 『每日一问』volatile干嘛的
- 『怎么用』装饰者模式
- 什么是贫血模型和充血模型?
- SSM框架整合,简单案例
- registration status: 204
- 在Ubuntu中安装MySQL并创建新用户
- @Scheduled源码解析
猜你喜欢
![[HCTF 2018]admin](/img/d7/f0155c72d3fbddf0a8c1796a179a0f.png)
[HCTF 2018]admin

Nacos启动报错Unable to start web server

有误差的字符串型时间比较方法String.compareTo

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

yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。
![[De1CTF 2019]SSRF Me](/img/12/44c37cc713b49172a10579c9628c94.png)
[De1CTF 2019]SSRF Me

『每日一问』ReentrantLock加锁解锁

将list集合的某一字段拼接单个String

API健康状态自检

activemq--死信队列
随机推荐
Interviewer: tell me the difference between redis and mongodb? [easy to understand]
[De1CTF 2019]SSRF Me
activemq--可持久化机制之JDBC代码
多态和接口
Thymeleaf 笔记
Wechat applet obtains the data of ---- onenet and controls the on-board LED of STM32
c语言中的六个存储类型:auto register static extern const volatile
redis的五种数据结构原理分析
BigDecimal 对数据进行四舍五入
Detailed explanation of pipeline pipeline mechanism in redis
Redis-哨兵,主从部署详细篇
实现简单的RESTful API服务器
idea 热部署
C#语言和SQL Server数据库技术
【Nacos】NacosClient在服务注册时做了什么
centos更改mysql数据库目录
[GYCTF2020]Node Game
Guangzhou has carried out in-depth "100 day action" to check the safety of self built commercial houses, and more than 2 million houses have been checked in two months
Bi business interview with data center and business intelligence (I): preparation for Industry and business research
『每日一问』怎么实现一个正确的双重检查锁定