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

导读:本篇文章讲解 详解next().nextLine().hasNext().hanNextLine(),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

目录

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 会停止读取

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

    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);
    }

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

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);
    }

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

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);
    }

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

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

(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().hasNext().hanNextLine()

这就会发现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().hasNext().hanNextLine()

可以发现在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().nextLine().hasNext().hanNextLine()

 可以发现每一个字母后都有空格,而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();
    }

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/87358.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!