当前位置:网站首页>Problems and solutions encountered in using nextline(), nextint() and next() in scanner
Problems and solutions encountered in using nextline(), nextint() and next() in scanner
2022-07-26 23:17:00 【Bathe in the wind and swim in the clouds】
One 、 problem
Code :
import java.util.ArrayList;
import java.util.Scanner;
public class JavaDemo {
public static void main(String[] args) {
// Read... From the keyboard
Scanner sc = new Scanner(System.in);
// establish ArrayList A collection of objects
ArrayList<Student> students = new ArrayList<>();
// Loop input 5 Student information , And deposit in ArrayList Collection
for (int i = 0; i < 5; i++) {
System.out.println(" Please enter the first " + (i + 1) + " Names of students :");
String name = sc.nextLine();
System.out.println(" Please enter the first " + (i + 1) + " Student gender :");
String gender = sc.nextLine();
System.out.println(" Please enter the first " + (i + 1) + " Age of students :");
int age = sc.nextInt();
Student student = new Student(name, gender, age);
students.add(student);
}
// Print
for (Student s : students) {
System.out.println(s);
}
}
}
The operation results are as follows :
As you can see from the results , Entering the second student name was skipped .
The reason is that nextLine() and nextInt() The ending way of is different ,nextLine() The method is to enter \n end ,nextInt() The method is Hit enter \n Or the space ends , Only read the input before entering int Type value , And continue to pass the following bytes .
This leads to nextLine() When the method receives the first loop nextInt() Enter after input \n, That is to say “23\n” Medium “\n”, Although enter \n No display , But there is still transmission , So enter the number 2 When there are student names, the program will read enter \n end , Make the next entry .
Two 、 use next() solve
To solve this problem , Just put nextLine() Switch to next() Just go , because next() End with a space , Cannot read carriage return \n Or space , And encounter the next carriage return \n Or the space will stop .
Code :
import java.util.ArrayList;
import java.util.Scanner;
public class JavaDemo {
public static void main(String[] args) {
// Read... From the keyboard
Scanner sc = new Scanner(System.in);
// establish ArrayList A collection of objects
ArrayList<Student> students = new ArrayList<>();
// Loop input 5 Student information , And deposit in ArrayList Collection
for (int i = 0; i < 5; i++) {
System.out.println(" Please enter the first " + (i + 1) + " Names of students :");
String name = sc.next();
System.out.println(" Please enter the first " + (i + 1) + " Student gender :");
String gender = sc.next();
System.out.println(" Please enter the first " + (i + 1) + " Age of students :");
int age = sc.nextInt();
Student student = new Student(name, gender, age);
students.add(student);
}
// Print
for (Student s : students) {
System.out.println(s);
}
}
}
The operation results are as follows :
边栏推荐
- 【HCIP】OSPF 路由计算
- HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由
- Is test development development development?
- Cheaper than seals, with a large space for shape explosion. Is there really no match for 200000 or so? Chang'an's new "King fried" is cost-effective
- 企业如何缓解物联网和工业物联网安全风险
- Monte Carlo search tree (UCT) based on confidence upper bound to realize four sub chess
- 逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!
- Eureka基本使用
- Security team: Recently, there is an rce vulnerability in the COREMAIL email client of windows, which may lead to the disclosure of the private key of the wallet
- [postgresql]postgresqlg使用enerate_series() 函数补全统计
猜你喜欢

Programmer growth chapter 29: how to motivate employees?

研究阿尔茨海默病最经典的Nature论文涉嫌造假

How to recover the original data when the U disk is damaged, and how to recover the damaged data when the U disk is damaged

Apifox -- a better API testing tool than postman

sql多表查询的练习
电脑开机后内存占用过高(50%以上)

逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!

Shardingsphere JDBC keyword problem

Basic use of gateway

Hcia-r & s self use notes (18) campus network architecture foundation, switch working principle, VLAN principle
随机推荐
Kingbasees SQL language reference manual of Jincang database (3.1.1.14. scope type)
Hcia-r & s self use notes (23) DHCP
2019 biometric forum successfully ended: these ten highlights should not be missed!
json格式化小工具--pyqt5实例
Restful interface specification
Product principles of non-financial decentralized application
8-其他编程语言--记录
The memory occupation of the computer is too high after it is turned on (more than 50%)
Will the approval in advance affect the formal approval?
MySQL random paging to get non duplicate data
比海豹便宜,造型炸裂空间大,20万左右真没对手?长安全新“王炸”这样选才划算
杰理下载器强制下载工具的使用介绍_AC695N696NAD14AD15全系列支持
Mate30 series release: how far can Huawei go in reconstructing images?
Security team: Recently, there is an rce vulnerability in the COREMAIL email client of windows, which may lead to the disclosure of the private key of the wallet
kalibr标定realsenseD435i --多相机标定
研究阿尔茨海默病最经典的Nature论文涉嫌造假
【HCIP】OSPF 关系建立
Openstack virtual machine network card is renamed cirename0
Cloud native microservices Chapter 1 server environment description
提前批到底影不影响正式批?