next()和nextLine()的区别详解
来源:互联网 发布:excel数据透视表分析 编辑:程序博客网 时间:2024/05/29 08:16
//next()和nextLine()的区别详解
/*next()方法在读取内容时,会过滤掉有效字符前面的无效字符,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next()方法会自动将其过滤掉;只有在读取到有效字符之后,next()方法才将其后的空格键、Tab键或Enter键等视为结束符;所以next()方法不能得到带空格的字符串。
*/
/*nextLine()方法字面上有扫描一整行的意思,它的结束符只能是Enter键,即nextLine()方法返回的是Enter键之前没有被读取的所有字符,它是可以得到带空格的字符串的。
*/
package java_eight;
import java.util.Scanner;
public class Test_nextLine {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入两个字符串作为next()的接收值");
/*参见下文输入输出图片,此处next()方法过滤了"ab"前面的空格,遇到"ab"后的空格后结束了输入,最后得到的结果是"ab",同样"cd"前的空格被next()方法当做无效字符过滤,但是"ef"前的空格并没有被next()方法读取,其会作为nextLine()的有效字符被nextLine()读取,直到nextLine()遇到Enter为止;,Enter会被nextLine()当做无效字符,接着str4开始读取"gh df"直到遇到Enter后结束*/
String str1=input.next();
String str2=input.next();
System.out.println("str1="+str1);
System.out.println("str2="+str2);
System.out.println("请输入两个字符串作为nextLine()的接收值");
String str3=input.nextLine();
String str4=input.nextLine();
System.out.println("str1="+str3);
System.out.println("str2="+str4);
}
}
- next()和nextLine()的区别详解
- next()和nextLine()的区别
- next()和nextLine()的区别
- next()和nextline()的区别
- next()和nextLine()的区别
- next()和nextLine()的区别
- next和nextLine的区别
- next和nextLine的区别
- next()和nextLine()的区别
- next()和nextLine()的区别
- nextLine和next的区别
- next()和nextLine()的区别
- next()和nextLine()区别
- next()和nextline()区别
- nextLine()和next()的区别和使用方法
- nextLine()和next()的区别和使用方法
- Scanner的next()和nextLine()的区别
- java中next()和nextLine()的区别
- DataGrid数据列表组件 使用手册
- POJ-1157-LITTLE SHOP OF FLOWERS
- CMMI--受管理级
- 第二章 类与对象
- C++函数
- next()和nextLine()的区别详解
- linux-清空文件内容的三种方法
- 第九期 使用 Keras 在游戏中实现自动驾驶 《显卡就是开发板》
- Python核心编程 第一章--正则表达式
- 111. Minimum Depth of Binary Tree
- android Pixelflinger
- 深度学习在信息隐藏中的应用(下)
- arm64_device_init
- 最小生成树算法