• Java中next()和nextLine()的区别(为什么nextLine()输入回车没显示)


    一、问题描述:

    前几天遇到了一个小问题:为了简化,我手打了一段简单的代码,如下:

    1. import java.util.Scanner;
    2. public class Test01 {
    3. public static void main(String[] args) {
    4. Scanner sc = new Scanner(System.in);
    5. System.out.println("输入一个数字:");
    6. int a = sc.nextInt();
    7. System.out.println("输入一个字符串:");
    8. String str = sc.nextLine(); //运行程序宛如跳过了这段代码一样
    9. System.out.println("输出的是:"+str);
    10. }
    11. }

    运行,输入2,然后点击回车,此时代码直接会运行结束。str无法接受任何字符串(在我们眼中宛如直接跳过了该条语句一样)。

    这是为何呢?nextLine()不是接受字符串吗?怎么不执行呢?

    二、原因分析:

    这里就要详细讲一下nextLine()在接受键盘输入的注意事项了。

    注意nextLine() 会接收回车字符(包含空格和Tab键)。

    基于这个特性,上述代码在输入2之后打了一个回车,nextInt()接收了2 这个数字之后碰到回车符结束,此时'\n'这个回车字符会留在缓冲区里。随后执行下一条语句nextLine(),nextLine()会接受(不排斥不忽略)这个回车字符,并且使得语句直接结束(nextLine()以回车符为结束)。 ==下面是nextLine()的源码:== 函数在接受回车之后直接返回。

    所以,在我们眼中:

    1. String str = sc.nextLine();

    宛如没有执行一样。

    解决方案1:

    既然我们知道了nextLine()的特性,那么,我们可以在nextInt()语句后面再加上一句nextLine()语句,用于“吃”掉这个输入缓冲区的'\n'。

    例如:

    1. import java.util.Scanner;
    2. public class Test01 {
    3. public static void main(String[] args) {
    4. Scanner sc = new Scanner(System.in);
    5. System.out.println("输入一个数字:");
    6. int a = sc.nextInt();
    7. System.out.println("输入一个字符串:");
    8. sc.nextLine(); //加上这条语句,用于吃掉'\n'
    9. String str = sc.nextLine();
    10. System.out.println("输出的是:"+str);
    11. }
    12. }

    解决方案2:

    在后面补上一条Scanner类里面的skip方法,用于跳过那个换行符。

    1. import java.util.Scanner;
    2. public class Test01 {
    3. public static void main(String[] args) {
    4. Scanner sc = new Scanner(System.in);
    5. System.out.println("输入一个数字:");
    6. int a = sc.nextInt();
    7. System.out.println("输入一个字符串:");
    8. sc.skip("\n"); //加个skip方法,用于跳过那个换行符。
    9. String str = sc.nextLine();
    10. System.out.println("输出的是:"+str);
    11. }
    12. }

    补充:

    ==next()和nextLine()的区别==:

    next和nextline方法的区别在于nextLine会接收回车字符(包含空格和Tab键)而next不会。

    next()方法是不接受回车字符的(包含空格和Tab键)!

    什么意思?拿上述的例子来说,要是把nextLine()换成next():

    1. import java.util.Scanner;
    2. public class Test01 {
    3. public static void main(String[] args) {
    4. Scanner sc = new Scanner(System.in);
    5. System.out.println("输入一个数字:");
    6. int a = sc.nextInt();
    7. System.out.println("输入一个字符串:");
    8. String str = sc.next(); //换成这个
    9. System.out.println("输出的是:"+str);
    10. }
    11. }

    程序是正常进行的,因为next()就算碰到了输入缓冲区里面的'\n'也会==忽略掉==(不接受)!!!

    最重要的一点是:nextInt、nextdoublie、nextfloat和next方法的效果是一样的,需要特别注意。

    总结

    所以说具体业务要具体分析,如果老铁们要输入一大行字符串(以空格分隔)的话,调用nextLine()。因为next()碰到空格就停止了,只截取有效部分,有时候不能满足业务需求。还有最后一点就是注意这个“吃掉”回车符的小技巧~

  • 相关阅读:
    BIT-5-操作符详解(C语言初阶学习)
    orcale 单表查询和多表联合查询
    Go 语言常用数据结构
    设计模式-原型模式
    Android13将Settings移植到AndroidStudio中
    丙二醇二乙酸酯(Propylene Glycol Diacetate)
    2022前端面试题上岸手册-前端工程化部分
    RK3588平台开发系列讲解(项目篇)实时显示摄像头
    jQuery网页开发案例:jQuery常用API--jQuery 元素操作
    【LMKD】十 lmkd进程查杀配置
  • 原文地址:https://blog.csdn.net/m0_57042151/article/details/128061696