详解next().nextLine().hasNext().hanNextLine()

目录

1.next()和nextLine()

1.1 next()

1.2 nextLine()

1.3 next()和nextLine()方法连用

2.hanNext()和hasNextLine()

2.1 hasNext()

2.2 hasNextLine()

2.3 特别强调有位博主这块讲的是真的细节


1.next()和nextLine()

nextLine和next都是读取输入台输入的字符。

1.1 next()

next()在读取到有效字符前,遇到空格、tab、回车等会自动忽略,直到读取有效字符后,再次遇到空格、回车、tab 会停止读取

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str1 =null ;
        System.out.println("输入的字符串为:");
        str1 = sc.next();
        System.out.println("输出的字符串为:");
        System.out.println(str1);
    }

 

1.2 nextLine()

nextLine()读取的是回车前的所有字符,包括空格、tab

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str1 =null ;
        System.out.println("输入的字符串为:");
        str1 = sc.nextLine();
        System.out.println("输出的字符串为:");
        System.out.println(str1);
    }

  

1.3 next()和nextLine()方法连用

(1)如果nextLine()在next()前面

    public static void main(String[] args) {
        String s1,s2;
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        s1=sc.nextLine();
        System.out.print("请输入第二个字符串:");

        s2=sc.next();
        System.out.println("输入的字符串是:"+s1+" "+s2);
    }

可以看到输出是没有问题的

(2)如果next()在nextLine()前面

    public static void main(String[] args) {
        String s1,s2;
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        s2=sc.next();
        System.out.print("请输入第二个字符串:");
        s1=sc.nextLine();

        System.out.println("输入的字符串是:"+s1+" "+s2);
    }

这就会发现next输入完成后,到nextLine就会自动把next去掉的回车读取出来 ,从而没办法输入s2.

⚜️ 那么如何解决这个问题呢

    public static void main(String[] args) {
        String s1,s2;
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        s1=sc.next();
        sc.nextLine();
        System.out.print("请输入第二个字符串:");
        s2=sc.nextLine();
        System.out.println("输入的字符串是:"+s1+" "+s2);
    }

可以发现在next后,连续使用了两次nextLine,第一个nextLine是为了把next去掉的回车读取了,第二个nextLine才是为了输入字符串

所以以后next和nextLine连用,并且next先使用的话,那就要两次nextLine



2.hanNext()和hasNextLine()

hanNext()和hanNextLine()都是用于判断有无键盘输入的,有则返回true,没有则阻塞

2.1 hasNext()

hasNext()经常用于判断是否还有输入的数据,也就是非空字符

比如说将hasNext()放在while()循环中,如果还要输入那就返回true,如果没有则阻塞(阻塞就是一直停留在判断阶段),由此来判断是否还有需要输入的数据。

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] str = new String[5];
        int i = 0;
        while (sc.hasNext()) {
            str[i] = sc.next();
            i++;
            for (int j = 0; j < i; j++) {
                System.out.println(str[j]);
            }
        }
    }

 

 可以发现每一个字母后都有空格,而next遇到空格后,停止读取,这时hasNext就体现出作用了,检查是否还有要输入的,

2.2 hasNextLine()

hasNextLine() 方法会根据行匹配模式去判断接下来是否有一行(包括空行),如果有,则返回true

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("使用nextline方式接收字符串:");
        //判断用户有没有输入字符串
        if(sc.hasNextLine()){
            String s = sc.nextLine();
            System.out.println("输出的内容为:"+s);
        }
        sc.close();
    }

 

2.3 特别强调有位博主这块讲的是真的细节

如果看完我前面写的,对这块还是有疑问,大家可以看下这个博主的这篇文章,写的真的特别好

hasNext、hasNextLine、next、nextLine保姆级详解

猜你喜欢

转载自blog.csdn.net/m0_58761900/article/details/125033719
今日推荐