当前位置:网站首页>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 :
边栏推荐
- The interviewer asked: this point of JS
- Domestic DRAM will be mass produced by the end of the year, but the road ahead is still long!
- TypeScript阶段学习
- Write golang simple C2 remote control based on grpc
- pgsql -&gt; flink cdc -&gt; flink -&gt; Mysql, if a PgSQL CDC
- Eureka基本使用
- Eureka basic use
- Huawei conspires to acquire Brazilian operators?
- DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主
- Regular expressions and bypass case recurrence
猜你喜欢
![[untitled]](/img/88/68952146da17a4a504ee7d1bc8858d.png)
[untitled]

数据库全栈工程师(DevDBOps)低首付、高回报,先就业后付款

App information reconnaissance & night God simulator burp packet capture configuration

ESMFold: AlphaFold2之后蛋白质结构预测的新突破

PostgreSQL 与 Navicat:数据库行业的中坚力量

Shardingsphere JDBC keyword problem

Vector execution engine framework gluten announced the official open source and appeared at spark technology summit

SQL multi table query exercise

Introduction to Nacos as a registry and configuration center - realize remote call, dynamically obtain configuration files and database configuration information

Professor Ashe, a Chinese scientist, made a positive response to the suspected fake Nature paper
随机推荐
电脑开机后内存占用过高(50%以上)
【flask高级】结合源码分析flask中的线程隔离机制
Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum
Weilai cup 2022 Niuke summer multi school training camp 2
Boss; Can flick CDC Oracle finish reading the full amount of data, just like directly fetching data from the database
HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由
Apifox -- a better API testing tool than postman
Customer case | student education relies on observation cloud to create a new ecosystem of observable Smart Education
【HCIP】OSPF 路由计算
企业数据治理面临的六大挑战!
The most classic Nature paper on Alzheimer's disease is suspected of fraud
Do you know the common core types of magnetic ring inductors?
org.yaml.snakeyaml.scanner. ScannerException: mapping values are not allowed here in ‘reader‘, line
The interviewer asked: this point of JS
Basic select statement
Shardingsphere JDBC keyword problem
Science | University of Washington uses AI and structural prediction to design new proteins
[hcip] OSPF external route introduction
Xu Li, CEO of Shangtang Technology: the company's valuation has exceeded $7billion, so we are not in a hurry to go public
Siliwei's counterattack: huiding's under screen optical fingerprint patent involved in the case was declared invalid