关于nextLine()和next()的使用
2015-12-08 21:33
281 查看
关键在于:next() 方法遇见第一个有效字符(非空格,换行符)时,开始扫描,当遇见第一个分隔符或结束符(空格或换行符)时,结束扫描。、
这时使用nextLine(),继续读,有可能读入第一个字符是空格或换行符。
在实现字符窗口的输入时,我个人更喜欢选择使用扫描器Scanner,它操作起来比较简单。
在写作业的过程中,我发现用Scanner实现 字符串的输入有两种方法,一种是next(),一种
是nextLine(),但是这两种方法 究竟有什么区别呢?
我查了一些资料总结如下:
next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab、
或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法
才将其后的输入的空格键、Tab或Enter键等视为分隔符或结束符。简单地说,next()查找并返回
来自此扫描器的下一下完整标记。完整标记的前后是与分隔模式的输入信息,所以next方法不能得到
带空格的字符串。
而nextLine()方法的结束符只是Enter键,即nextLine()方法返回的是Enter键之前的所有字符,它是
可以得到带空格的字符串的。
例子一:
鉴于以上两种方法的区别,同学样一定要注意next()方法 与nextLine()方法 的连用,举例如下:
package com.tarena.corejava;
import java.util.Scanner;
public class TestScanner2 {
public static void main(String[] args) {
String s1,s2;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个字符串:");
//s1 = sc.nextLine();
s1=sc.next();
System.out.println("请输入第二个字符:");
//s2 = sc.next();
s2=sc.nextLine();
System.out.println("输入的字符串是:"+s1+" "+s2);
}
}
可以看到,nextLine()自动读取了被next()去掉的Enter作为他的结束符,所以没办法给s2键盘验证,我发现
其它的next的方法 ,如dobul nextDouble(),float next Float(),int nextInt()等与nextLine()连用时都存在
这个问题,解决办法是:在第一个next(),nextDouble(),nextFloat(),nextInt()等语句后加一个nextLine()语句,
将next()去掉的Enter结束符过滤掉,例如上面的程序改写为:
public static void main(String[] args) {
String s1,s2;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个字符串:");
//s1 = sc.nextLine();
s1=sc.next();
sc.nextLine();
System.out.println("请输入第二个字符:");
//s2 = sc.next();
s2=sc.nextLine();
System.out.println("输入的字符串是:"+s1+" "+s2);
}
就可以了。
例子二:
package com.tarena.corejava;
import java.util.Scanner;
public class TestScanner {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("test next:输入字符串:");
String s2 = scan.next();
System.out.println("得到的字符串是:"+s2);
System.out.println("test nextLine:输入字符串:");
String s3 = scan.nextLine();
System.out.println("得到的字符串是:"+s3+"长度是:"+s3.length());
}
}
英文例子:
bb9c
very time I used the Scanner class. At the time, it seemed like it behaved erratically and no matter how much debugging I did, it was always wrong.
The mistake I was making was that I failed to realize that whenever I used a .next call, like nextInt or nextDouble, the Scanner would stay on the same line. If then, I wanted to scan a new input that was on a different line, the Scanner will appear to skip
it. What happens is that the Scanner is actually scanning the invisible newline character!
So, whenever you want to Scan numbers with a .next() call, make sure you add an empty .nextLine() at the end so that the Scanner moves to the correct position.
Ex:
package com.tarena.corejava;
import java.util.Scanner;
public class MyScanner {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
double grade=0;
String name=null;
System.out.print("Please enter the grade: ");
grade = scn.nextDouble(); //Could also be scn.next(), scn.nextInt(), and so on
//Add an empy .nextLine() to skip the invisible newline character
scn.nextLine(); //Try commenting this line out with '//' to see what happens
System.out.print("Please enter name: ");
name = scn.nextLine();
System.out.println("Name: "+name+", grade: "+grade);
}
}
Sample run without the
empty scn.nextLine:
Sample run with the
empty scn.nextLine:
Beautiful!
解决方案一:
当next () 与 nextLine() 一起使用时要注意:
next() 在 nextLine 之前使用时,next 扫描空格或换行之前的字符,并没有读入"\n" , 而nextLine 则要从"\n" 开始读入,所以得不到数据、
正确方法:
int n = cin.nextInt();
while(str = cin.nextLine().equles(""))
{
}
原文:http://blog.sina.com.cn/s/blog_81547cad01018mnd.html
这时使用nextLine(),继续读,有可能读入第一个字符是空格或换行符。
在实现字符窗口的输入时,我个人更喜欢选择使用扫描器Scanner,它操作起来比较简单。
在写作业的过程中,我发现用Scanner实现 字符串的输入有两种方法,一种是next(),一种
是nextLine(),但是这两种方法 究竟有什么区别呢?
我查了一些资料总结如下:
next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab、
或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法
才将其后的输入的空格键、Tab或Enter键等视为分隔符或结束符。简单地说,next()查找并返回
来自此扫描器的下一下完整标记。完整标记的前后是与分隔模式的输入信息,所以next方法不能得到
带空格的字符串。
而nextLine()方法的结束符只是Enter键,即nextLine()方法返回的是Enter键之前的所有字符,它是
可以得到带空格的字符串的。
例子一:
鉴于以上两种方法的区别,同学样一定要注意next()方法 与nextLine()方法 的连用,举例如下:
package com.tarena.corejava;
import java.util.Scanner;
public class TestScanner2 {
public static void main(String[] args) {
String s1,s2;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个字符串:");
//s1 = sc.nextLine();
s1=sc.next();
System.out.println("请输入第二个字符:");
//s2 = sc.next();
s2=sc.nextLine();
System.out.println("输入的字符串是:"+s1+" "+s2);
}
}
可以看到,nextLine()自动读取了被next()去掉的Enter作为他的结束符,所以没办法给s2键盘验证,我发现
其它的next的方法 ,如dobul nextDouble(),float next Float(),int nextInt()等与nextLine()连用时都存在
这个问题,解决办法是:在第一个next(),nextDouble(),nextFloat(),nextInt()等语句后加一个nextLine()语句,
将next()去掉的Enter结束符过滤掉,例如上面的程序改写为:
public static void main(String[] args) {
String s1,s2;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个字符串:");
//s1 = sc.nextLine();
s1=sc.next();
sc.nextLine();
System.out.println("请输入第二个字符:");
//s2 = sc.next();
s2=sc.nextLine();
System.out.println("输入的字符串是:"+s1+" "+s2);
}
就可以了。
例子二:
package com.tarena.corejava;
import java.util.Scanner;
public class TestScanner {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("test next:输入字符串:");
String s2 = scan.next();
System.out.println("得到的字符串是:"+s2);
System.out.println("test nextLine:输入字符串:");
String s3 = scan.nextLine();
System.out.println("得到的字符串是:"+s3+"长度是:"+s3.length());
}
}
英文例子:
Java Scanner not working, difference between next() and nextLine()
When I was a newbie programmer, I used to be frustrated a lot ebb9c
very time I used the Scanner class. At the time, it seemed like it behaved erratically and no matter how much debugging I did, it was always wrong.
The mistake I was making was that I failed to realize that whenever I used a .next call, like nextInt or nextDouble, the Scanner would stay on the same line. If then, I wanted to scan a new input that was on a different line, the Scanner will appear to skip
it. What happens is that the Scanner is actually scanning the invisible newline character!
So, whenever you want to Scan numbers with a .next() call, make sure you add an empty .nextLine() at the end so that the Scanner moves to the correct position.
Ex:
package com.tarena.corejava;
import java.util.Scanner;
public class MyScanner {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
double grade=0;
String name=null;
System.out.print("Please enter the grade: ");
grade = scn.nextDouble(); //Could also be scn.next(), scn.nextInt(), and so on
//Add an empy .nextLine() to skip the invisible newline character
scn.nextLine(); //Try commenting this line out with '//' to see what happens
System.out.print("Please enter name: ");
name = scn.nextLine();
System.out.println("Name: "+name+", grade: "+grade);
}
}
Sample run without the
empty scn.nextLine:
Please enter the grade: 76 Please enter name: Name: , grade: 76.0
Sample run with the
empty scn.nextLine:
Please enter the grade: 100 Please enter name: Rommel Name: Rommel, grade: 100.0
Beautiful!
解决方案一:
当next () 与 nextLine() 一起使用时要注意:
next() 在 nextLine 之前使用时,next 扫描空格或换行之前的字符,并没有读入"\n" , 而nextLine 则要从"\n" 开始读入,所以得不到数据、
正确方法:
int n = cin.nextInt();
while(str = cin.nextLine().equles(""))
{
}
原文:http://blog.sina.com.cn/s/blog_81547cad01018mnd.html
相关文章推荐
- Round2 - Probability
- Struts2的国际化
- SUSE Linux Enterprise Server设置IP地址、网关、DNS
- UITableViewCell contentView layoutSubviews 死循环
- Struts2的国际化
- LAMP 2.0Apache日志切割
- 整形子类(int subtypes)
- 使用Github Pages建独立博客
- 【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- 字典
- Tomcat 服务器 web.xml常用配置
- linux学习(1)--文件
- js实现卡片式项目管理界面UI设计效果
- 数组
- POJ 2887 Big String(块状链表)
- cuda编程学习1——hello world!
- c运行库、c标准库、windows API的区别和联系
- 【Android游戏开发十五】关于Android 游戏开发中 OnTouchEvent() 触屏事件的性能优化笔记!
- Handler的内存泄露几解决办法以及闪屏效果的实现
- html5结合JavaScript实现龙播图