search
HomeJavajavaTutorialHow to use the scan.next() and scan.nextline() functions in java

    scan.next()与scan.nextline()函数的使用及区别

    今天在做牛客网编程练习题“length of last word”时,当编写实现代码时,使用split()函数对输入的字符串进行按空格符分割,确遇到了”奇葩“的问题,每次只能得到第一个字符串。

    开始以为是split()函数用错了,查了资料确定无误后,觉得应该是输入的有问题。

    于是进行了下面的实验:

    import java.util.Scanner;
    
    public class Solution {
        
    	public static void main(String[] args) {
    		String s_next = "";
    		String s_nextLine = "";
    		int count_next = 0; // 计数
    		int count_nextLine = 0; // 计数
    		Scanner scan = new Scanner(System.in);  
    		System.out.println("请输入第一个字符串:");	
    		s_nextLine = scan.nextLine(); // 此处使用nextLine(),便于对比
    		System.out.println("请输入第二个字符串:");	
    		s_next = scan.next(); // 第一次使用的next();
    		
    		scan.close();
    		
    		String [] split_next = s_next.split("\\s+");
    		String [] split_nextLine = s_nextLine.split("\\s+");
    		
    		for(String s : split_next)
    			System.out.println("子串next: "+ count_next++ +": "+ s + " 长度: " + s.length()+ '\n');
    		
    		for(String s : split_nextLine)
    			System.out.println("子串nextLine: "+ count_nextLine++ +": "+ s + " 长度: " + s.length()+ '\n');
    		
    	}
    
    }

    测试结果

    也验证了我的猜想

    How to use the scan.next() and scan.nextline() functions in java

    注意:

    自省,也希望能对大家有所帮助,少走弯路。

    • 用 Scanner 实现字符串的输入有两种方法,一种是next(),一种nextLine();

    • next() 一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next() 方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符。

    • nextLine()方法的结束符只是Enter键。

    简言之,next方法不能得到带空格的字符串,而nextLine()方法返回的是Enter键之前的所有字符,因此出现了上面测试样例的结果。(ps.一定要注意!)

    Scanner类的next()和nextLine()方法

    java的Scanner类可以用来接收键盘输入的数据。next()和nextLine()方法用来接收字符串,next()方法接收字符串时遇到空格或回车结束输入,而nextLine()方法可以接收空格,最后输入回车才结束。下面用实例演示

    两者的区别:

    next()方法

    package scanner;
    import java.util.Scanner;
    public class Scan {
    
    	public static void main(String[] args) {
    		
    		String a,b;
    		Scanner sc=new Scanner(System.in);
    		System.out.println("next()方法接收字符串:");
    		a=sc.next();
    		System.out.println(a);
    	}
    
    }

    运行结果截图:

    How to use the scan.next() and scan.nextline() functions in java

    nextLine()方法

    package scanner;
    import java.util.Scanner;
    public class Scan {
    	public static void main(String[] args) {
    		String a,b;
    		Scanner sc=new Scanner(System.in);
    		System.out.println("nextLine()方法接收字符串:");
    		b=sc.nextLine();
    	    System.out.println(b);
    	}
    }

    运行结果截图:

    How to use the scan.next() and scan.nextline() functions in java

    两个方法一起用可能会出错:

    package scanner;
    import java.util.Scanner;
    public class Scan {
    	public static void main(String[] args) {
    		String a,b;
    		Scanner sc=new Scanner(System.in);
    		System.out.println("next()方法接收字符串:");
    		a=sc.next();
    		System.out.println(a);
    		System.out.println("nextLine()方法接收字符串:");
    		b=sc.nextLine();
    	    System.out.println(b);
    	}
    }

    运行结果截图:

    How to use the scan.next() and scan.nextline() functions in java

    这时程序已结束运行,不能再输入。原因是next()方法遇到回车结束输入,却把最后的回车符留给了nextLine(),nextLine()方法接收了一个空字符串。

    解决方法是next()方法后面再加一个nextLine()用来接收回车符,代码如下:

    package scanner;
    import java.util.Scanner;
    public class Scan {
    	public static void main(String[] args) {
    		String a,b;
    		Scanner sc=new Scanner(System.in);
    		System.out.println("next()方法接收字符串:");
    		a=sc.next();
    		System.out.println(a);
    		a=sc.nextLine();//接收回车符
    		System.out.println("nextLine()方法接收字符串:");
    		b=sc.nextLine();
    	    System.out.println(b);
    	}
    }

    运行结果截图:

    How to use the scan.next() and scan.nextline() functions in java

    The above is the detailed content of How to use the scan.next() and scan.nextline() functions in java. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

    Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

    完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

    一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

    Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

    详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

    本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

    Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

    java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

    封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools