Scanner中nextInt()、nextLine()方法的使用注意事项 笔记
2018-03-17 00:10
429 查看
一、nextInt()、nextLine()区别:
1.nextInt()只会读取数值,剩下"\n"还没有读取,并将cursor放在本行中。
2.nextLine()会读取"\n",并结束(nextLine() reads till the end of line \n)。3.如果想要在nextInt()后读取一行,就得在nextInt()之后额外加上cin.nextLine(),/**
*
*/
package com.hqy.test;
import java.util.Scanner;
/**
* @author hadoop
*
*/
public class scannertest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inScanner=new Scanner(System.in);
int m=inScanner.nextInt();
String string=inScanner.nextLine();
System.out.println("OK");
}
}从下图可以看出:
从下图看出:String string=inScanner.nextLine();
该行并没有被执行,nextInt()只会读取数值,剩下"\n"还没有读取,并将cursor放在本行中。所以,inScanner.nextLine()直接读取“\n”并结束了。
二、要想在nextInt()后读取一行则要,载加上:in.nextLine()将nextInt()留下的“\n”读取掉。如下:import java.util.Scanner;
/**
* @author hadoop
*
*/
public class scannertest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inScanner=new Scanner(System.in);
int m=inScanner.nextInt();
inScanner.nextLine();
String string=inScanner.nextLine();
System.out.println("OK");
}
}
三、读取一个整数n,并在下一行中读取n个由空格隔开的整数的方法:
方法一:/**
*
*/
package com.hqy.test;
import java.util.Scanner;
/**
* @author hadoop
*
*/
public class scannertest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inScanner=new Scanner(System.in);
while(inScanner.hasNext()){
int m=inScanner.nextInt();
inScanner.nextLine();//去掉换行
String[] strings=inScanner.nextLine().trim().split(" ");
for(String str:strings){
System.out.print(str);
}
System.out.println();
}
}
}
方法二:/**
*
*/
package com.hqy.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author hadoop
*
*/
public class scannertest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inScanner=new Scanner(System.in);
while(inScanner.hasNext()){
int m=inScanner.nextInt();
List<Integer> list=new ArrayList<>();
for(int i=0;i<m;i++){
list.add(inScanner.nextInt());
}
for(Integer str:list){
System.out.print(str);
}
System.out.println();
}
}
}
注意:inScanner.nextInt()只读取数字
1.nextInt()只会读取数值,剩下"\n"还没有读取,并将cursor放在本行中。
2.nextLine()会读取"\n",并结束(nextLine() reads till the end of line \n)。3.如果想要在nextInt()后读取一行,就得在nextInt()之后额外加上cin.nextLine(),/**
*
*/
package com.hqy.test;
import java.util.Scanner;
/**
* @author hadoop
*
*/
public class scannertest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inScanner=new Scanner(System.in);
int m=inScanner.nextInt();
String string=inScanner.nextLine();
System.out.println("OK");
}
}从下图可以看出:
从下图看出:String string=inScanner.nextLine();
该行并没有被执行,nextInt()只会读取数值,剩下"\n"还没有读取,并将cursor放在本行中。所以,inScanner.nextLine()直接读取“\n”并结束了。
二、要想在nextInt()后读取一行则要,载加上:in.nextLine()将nextInt()留下的“\n”读取掉。如下:import java.util.Scanner;
/**
* @author hadoop
*
*/
public class scannertest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inScanner=new Scanner(System.in);
int m=inScanner.nextInt();
inScanner.nextLine();
String string=inScanner.nextLine();
System.out.println("OK");
}
}
三、读取一个整数n,并在下一行中读取n个由空格隔开的整数的方法:
方法一:/**
*
*/
package com.hqy.test;
import java.util.Scanner;
/**
* @author hadoop
*
*/
public class scannertest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inScanner=new Scanner(System.in);
while(inScanner.hasNext()){
int m=inScanner.nextInt();
inScanner.nextLine();//去掉换行
String[] strings=inScanner.nextLine().trim().split(" ");
for(String str:strings){
System.out.print(str);
}
System.out.println();
}
}
}
方法二:/**
*
*/
package com.hqy.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author hadoop
*
*/
public class scannertest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inScanner=new Scanner(System.in);
while(inScanner.hasNext()){
int m=inScanner.nextInt();
List<Integer> list=new ArrayList<>();
for(int i=0;i<m;i++){
list.add(inScanner.nextInt());
}
for(Integer str:list){
System.out.print(str);
}
System.out.println();
}
}
}
注意:inScanner.nextInt()只读取数字
相关文章推荐
- web前台工作笔记(时间戳、js中clone的使用、js中动态填充数据注意事项、前台查错方法的学习)
- iPhone开发笔记(20)EGOImageView的使用方法及注意事项
- fgets()函数的使用方法及注意事项(笔记)
- iPhone开发笔记(20)EGOImageView的使用方法及注意事项
- C++中ifstream使用笔记(一)(常用方法和注意事项)
- C++中typename关键字的使用方法和注意事项
- Java基础:String类之常用方法及使用注意事项
- 使用SPView时的注意事项和问题,及解决方法?
- CTE的使用方法和注意事项
- C++中typename关键字的使用方法和注意事项
- Scanner类中next()和nextLine()方法连用时注意事项
- ScrolledComposite使用方法及注意事项
- 学习笔记---将Asp.Net网站发布到IIS的四种方法及注意事项
- 使用ResultSet的geRow()方法的注意事项
- [学习笔记]寄存器使用注意事项
- SharePoint中使用SilverLight的注意事项及SilverLight显示空白的解决方法
- 分页解决方案 之 分页算法——Pager_SQL的详细使用方法和注意事项
- 分页解决方案 之 分页算法——Pager_SQL的详细使用方法和注意事项
- cvFloodFill的使用方法和注意事项。
- jdom中使用setAttribute(Attribute attribute)方法设置属性的注意事项